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
|
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include "platform.c"
void PaintMask(float *mask, float x, float y, float color, float size) {
for (int j = y - size; j < y + size; j++) {
for (int i = x - size; i < x + size; i++) {
float dj = y - j;
float di = x - i;
if (i > 0 && i < WINDOW_W && j > 0 && j < WINDOW_H
&& dj * dj + di * di <= size * size / 2) {
size_t index = i + j * WINDOW_W;
mask[index] = color;
}
}
}
}
void GameMain() {
// Terrain generation
float *mask = calloc(WINDOW_W * WINDOW_H, sizeof(float));
for (int i = 0; i < 100 * WINDOW_W; i++) {
mask[i] = 1.0;
}
// Player variables
float player_x = 100;
float player_y = 500;
float player_vy = 0;
float player_ay = -9.8;
// Timer variables
float old_t = 0.0;
// Event handling
int exit = 0;
while (!exit) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
exit = 1;
}
}
int mouse_x, mouse_y;
Uint32 mouse_state = SDL_GetMouseState(&mouse_x, &mouse_y);
float size = 20;
if (mouse_state & SDL_BUTTON_LMASK) {
PaintMask(mask, mouse_x, WINDOW_H - mouse_y, 1.0, size);
} else if (mouse_state & SDL_BUTTON_RMASK) {
PaintMask(mask, mouse_x, WINDOW_H - mouse_y, 0.0, size);
}
// Time delta calculation
double new_t = GetCurrentTimestamp();
double dt = new_t - old_t;
old_t = new_t;
// Player logic
if (mask[(int)player_x + (int)player_y * WINDOW_W] == 0.0) {
player_y += player_vy * dt;
player_vy += player_ay * dt;
} else {
player_y += 1;
player_vy = 0;
}
RenderScene(mask, player_x, player_y);
SDL_Delay(1);
}
}
int main() {
PlatformMain(GameMain);
return 0;
}
|