This repository has been archived by the owner on Aug 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 27
/
PlayMode.cpp
211 lines (184 loc) · 5.8 KB
/
PlayMode.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
#include "PlayMode.hpp"
//for the GL_ERRORS() macro:
#include "gl_errors.hpp"
//for glm::value_ptr() :
#include <glm/gtc/type_ptr.hpp>
#include <random>
PlayMode::PlayMode() {
//TODO:
// you *must* use an asset pipeline of some sort to generate tiles.
// don't hardcode them like this!
// or, at least, if you do hardcode them like this,
// make yourself a script that spits out the code that you paste in here
// and check that script into your repository.
//Also, *don't* use these tiles in your game:
{ //use tiles 0-16 as some weird dot pattern thing:
std::array< uint8_t, 8*8 > distance;
for (uint32_t y = 0; y < 8; ++y) {
for (uint32_t x = 0; x < 8; ++x) {
float d = glm::length(glm::vec2((x + 0.5f) - 4.0f, (y + 0.5f) - 4.0f));
d /= glm::length(glm::vec2(4.0f, 4.0f));
distance[x+8*y] = uint8_t(std::max(0,std::min(255,int32_t( 255.0f * d ))));
}
}
for (uint32_t index = 0; index < 16; ++index) {
PPU466::Tile tile;
uint8_t t = uint8_t((255 * index) / 16);
for (uint32_t y = 0; y < 8; ++y) {
uint8_t bit0 = 0;
uint8_t bit1 = 0;
for (uint32_t x = 0; x < 8; ++x) {
uint8_t d = distance[x+8*y];
if (d > t) {
bit0 |= (1 << x);
} else {
bit1 |= (1 << x);
}
}
tile.bit0[y] = bit0;
tile.bit1[y] = bit1;
}
ppu.tile_table[index] = tile;
}
}
//use sprite 32 as a "player":
ppu.tile_table[32].bit0 = {
0b01111110,
0b11111111,
0b11111111,
0b11111111,
0b11111111,
0b11111111,
0b11111111,
0b01111110,
};
ppu.tile_table[32].bit1 = {
0b00000000,
0b00000000,
0b00011000,
0b00100100,
0b00000000,
0b00100100,
0b00000000,
0b00000000,
};
//makes the outside of tiles 0-16 solid:
ppu.palette_table[0] = {
glm::u8vec4(0x00, 0x00, 0x00, 0x00),
glm::u8vec4(0x00, 0x00, 0x00, 0xff),
glm::u8vec4(0x00, 0x00, 0x00, 0x00),
glm::u8vec4(0x00, 0x00, 0x00, 0xff),
};
//makes the center of tiles 0-16 solid:
ppu.palette_table[1] = {
glm::u8vec4(0x00, 0x00, 0x00, 0x00),
glm::u8vec4(0x00, 0x00, 0x00, 0x00),
glm::u8vec4(0x00, 0x00, 0x00, 0xff),
glm::u8vec4(0x00, 0x00, 0x00, 0xff),
};
//used for the player:
ppu.palette_table[7] = {
glm::u8vec4(0x00, 0x00, 0x00, 0x00),
glm::u8vec4(0xff, 0xff, 0x00, 0xff),
glm::u8vec4(0x00, 0x00, 0x00, 0xff),
glm::u8vec4(0x00, 0x00, 0x00, 0xff),
};
//used for the misc other sprites:
ppu.palette_table[6] = {
glm::u8vec4(0x00, 0x00, 0x00, 0x00),
glm::u8vec4(0x88, 0x88, 0xff, 0xff),
glm::u8vec4(0x00, 0x00, 0x00, 0xff),
glm::u8vec4(0x00, 0x00, 0x00, 0x00),
};
}
PlayMode::~PlayMode() {
}
bool PlayMode::handle_event(SDL_Event const &evt, glm::uvec2 const &window_size) {
if (evt.type == SDL_KEYDOWN) {
if (evt.key.keysym.sym == SDLK_LEFT) {
left.downs += 1;
left.pressed = true;
return true;
} else if (evt.key.keysym.sym == SDLK_RIGHT) {
right.downs += 1;
right.pressed = true;
return true;
} else if (evt.key.keysym.sym == SDLK_UP) {
up.downs += 1;
up.pressed = true;
return true;
} else if (evt.key.keysym.sym == SDLK_DOWN) {
down.downs += 1;
down.pressed = true;
return true;
}
} else if (evt.type == SDL_KEYUP) {
if (evt.key.keysym.sym == SDLK_LEFT) {
left.pressed = false;
return true;
} else if (evt.key.keysym.sym == SDLK_RIGHT) {
right.pressed = false;
return true;
} else if (evt.key.keysym.sym == SDLK_UP) {
up.pressed = false;
return true;
} else if (evt.key.keysym.sym == SDLK_DOWN) {
down.pressed = false;
return true;
}
}
return false;
}
void PlayMode::update(float elapsed) {
//slowly rotates through [0,1):
// (will be used to set background color)
background_fade += elapsed / 10.0f;
background_fade -= std::floor(background_fade);
constexpr float PlayerSpeed = 30.0f;
if (left.pressed) player_at.x -= PlayerSpeed * elapsed;
if (right.pressed) player_at.x += PlayerSpeed * elapsed;
if (down.pressed) player_at.y -= PlayerSpeed * elapsed;
if (up.pressed) player_at.y += PlayerSpeed * elapsed;
//reset button press counters:
left.downs = 0;
right.downs = 0;
up.downs = 0;
down.downs = 0;
}
void PlayMode::draw(glm::uvec2 const &drawable_size) {
//--- set ppu state based on game state ---
//background color will be some hsv-like fade:
ppu.background_color = glm::u8vec4(
std::min(255,std::max(0,int32_t(255 * 0.5f * (0.5f + std::sin( 2.0f * M_PI * (background_fade + 0.0f / 3.0f) ) ) ))),
std::min(255,std::max(0,int32_t(255 * 0.5f * (0.5f + std::sin( 2.0f * M_PI * (background_fade + 1.0f / 3.0f) ) ) ))),
std::min(255,std::max(0,int32_t(255 * 0.5f * (0.5f + std::sin( 2.0f * M_PI * (background_fade + 2.0f / 3.0f) ) ) ))),
0xff
);
//tilemap gets recomputed every frame as some weird plasma thing:
//NOTE: don't do this in your game! actually make a map or something :-)
for (uint32_t y = 0; y < PPU466::BackgroundHeight; ++y) {
for (uint32_t x = 0; x < PPU466::BackgroundWidth; ++x) {
//TODO: make weird plasma thing
ppu.background[x+PPU466::BackgroundWidth*y] = ((x+y)%16);
}
}
//background scroll:
ppu.background_position.x = int32_t(-0.5f * player_at.x);
ppu.background_position.y = int32_t(-0.5f * player_at.y);
//player sprite:
ppu.sprites[0].x = int8_t(player_at.x);
ppu.sprites[0].y = int8_t(player_at.y);
ppu.sprites[0].index = 32;
ppu.sprites[0].attributes = 7;
//some other misc sprites:
for (uint32_t i = 1; i < 63; ++i) {
float amt = (i + 2.0f * background_fade) / 62.0f;
ppu.sprites[i].x = int8_t(0.5f * PPU466::ScreenWidth + std::cos( 2.0f * M_PI * amt * 5.0f + 0.01f * player_at.x) * 0.4f * PPU466::ScreenWidth);
ppu.sprites[i].y = int8_t(0.5f * PPU466::ScreenHeight + std::sin( 2.0f * M_PI * amt * 3.0f + 0.01f * player_at.y) * 0.4f * PPU466::ScreenWidth);
ppu.sprites[i].index = 32;
ppu.sprites[i].attributes = 6;
if (i % 2) ppu.sprites[i].attributes |= 0x80; //'behind' bit
}
//--- actually draw ---
ppu.draw(drawable_size);
}