summaryrefslogtreecommitdiff
path: root/main.c
blob: 9bcd467e9a7d225f92dd990cc355d8945c3b7cd5 (plain)
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <SDL2/SDL.h>

#define GRAVITY -9.8
#define PI 3.141592
#define WINDOW_W 1280
#define WINDOW_H 720
#define WALK_VEL 20

typedef struct {
	float x;
	float y;
	float vy;
	float angle;
	float power;
} Cannon;

typedef struct {
	float x;
	float y;
	float vy;
	float vx;
	int alive;
} Projectile;

static inline double GetCurrentTimestamp() {
	return (double) SDL_GetTicks64() / 1000.0;
}

SDL_Texture *CreateTiledTexture(SDL_Renderer *context, const char *filename, int width, int height) {
	SDL_Surface *texture_surface = SDL_LoadBMP(filename);
	SDL_Texture *texture_tile = SDL_CreateTextureFromSurface(context, texture_surface);
	int tile_w = texture_surface->w;
	int tile_h = texture_surface->h;
	SDL_FreeSurface(texture_surface);

	SDL_Texture *texture = SDL_CreateTexture(context,
			SDL_PIXELFORMAT_RGBA8888,
			SDL_TEXTUREACCESS_TARGET,
			width, height);
	SDL_SetRenderTarget(context, texture);
	for (int y = 0; y < height; y += tile_h) {
		for (int x = 0; x < width; x += tile_w) {
			SDL_Rect tile_rect = { x, y, tile_w, tile_h };
			SDL_RenderCopy(context, texture_tile, 0, &tile_rect);
		}
	}
	SDL_DestroyTexture(texture_tile);
	SDL_SetRenderTarget(context, 0);
	return texture;
}

void PaintMask(float *mask, int width, int height, 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 < width && j > 0 && j < height
					&& dj * dj + di * di <= size * size / 2) {
				size_t index = i + j * width;
				mask[index] = color;
			}
		}
	}
}

void GameMain(SDL_Renderer *context) {
	SDL_Texture *background = CreateTiledTexture(context, "background.bmp", WINDOW_W, WINDOW_H);

	SDL_Texture *foreground = CreateTiledTexture(context, "foreground.bmp", WINDOW_W, WINDOW_H);
	SDL_SetTextureBlendMode(foreground, SDL_BLENDMODE_MOD);

	SDL_Texture *terrain = SDL_CreateTexture(context,
			SDL_PIXELFORMAT_RGBA8888,
			SDL_TEXTUREACCESS_TARGET,
			WINDOW_W, WINDOW_H);
	SDL_SetTextureBlendMode(terrain, SDL_BLENDMODE_BLEND);

	SDL_Texture *mask_texture = SDL_CreateTexture(context,
			SDL_PIXELFORMAT_RGBA8888,
			SDL_TEXTUREACCESS_STREAMING,
			WINDOW_W, WINDOW_H);

	float *mask = calloc(WINDOW_W * WINDOW_H, sizeof(float));
	for (int i = 0; i < 100 * WINDOW_W; i++) {
		mask[i] = 1.0;
	}

	Cannon player = {0};
	player.x = 100;
	player.y = 500;
	player.power = 100;

	Projectile projectile = {0};

	float old_t = 0.0;

	int exit = 0;
	while (!exit) {
		double new_t = GetCurrentTimestamp();
		double dt = (new_t - old_t) * 10;
		old_t = new_t;

		SDL_Event event;
		while (SDL_PollEvent(&event)) {
			if (event.type == SDL_QUIT) {
				exit = 1;
			} else if (event.type == SDL_KEYDOWN && event.key.state == SDL_PRESSED && event.key.keysym.sym == SDLK_SPACE && !projectile.alive) {
				projectile.alive = 1;
				projectile.x = player.x;
				projectile.y = player.y;
				projectile.vy = sin(player.angle) * player.power;
				projectile.vx = cos(player.angle) * player.power;
			}
		}
		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, WINDOW_W, WINDOW_H, mouse_x, WINDOW_H - mouse_y, 1.0, size);
		} else if (mouse_state & SDL_BUTTON_RMASK) {
			PaintMask(mask, WINDOW_W, WINDOW_H, mouse_x, WINDOW_H - mouse_y, 0.0, size);
		}

		const Uint8 *keys = SDL_GetKeyboardState(0);
		if (keys[SDL_SCANCODE_D] && player.x < WINDOW_W) {
			player.x += WALK_VEL * dt;
		}
		if (keys[SDL_SCANCODE_A] && player.x >= 0) {
			player.x -= WALK_VEL * dt;
		}
		if (keys[SDL_SCANCODE_W] && player.angle < PI / 2) {
			player.angle += PI / 180 * dt;
		}
		if (keys[SDL_SCANCODE_S] && player.angle > 0) {
			player.angle -= PI / 180 * dt;
		}

		if (player.y >= WINDOW_H) {
			player.y  = WINDOW_H - 1;
			player.vy = 0;
		}
		if (mask[(int)player.x + (int)player.y * WINDOW_W] == 0.0 &&
				player.y >= 0) {
			player.y += player.vy * dt;
			player.vy += GRAVITY * dt;
			while (mask[(int)player.x + (int)player.y * WINDOW_W] != 0.0) {
				player.y += 1;
				player.vy = 0;
			}
		}

		if (projectile.alive) {
			if (projectile.y >= 0 && projectile.y < WINDOW_H &&
					projectile.x >= 0 && projectile.x < WINDOW_W &&
					mask[(int)projectile.x + (int)projectile.y * WINDOW_W] == 0.0) {
				projectile.x += projectile.vx * dt;
				projectile.y += projectile.vy * dt;
				projectile.vy += GRAVITY * dt;
			} else {
				projectile.alive = 0;
				PaintMask(mask, WINDOW_W, WINDOW_H, projectile.x, projectile.y, 0.0, 40);
			}
		}

		void *pixels;
		int pitch;
		SDL_LockTexture(mask_texture, 0, &pixels, &pitch);
		Uint32 *p = (Uint32 *) pixels;
		for (int j = 0; j < WINDOW_H; j++) {
			for (int i = 0; i < WINDOW_W; i++) {
				p[i + j * WINDOW_W] = mask[i + (WINDOW_H - j) * WINDOW_W] ? 0xffffffff : 0x00000000;
			}
		}
		SDL_UnlockTexture(mask_texture);

		SDL_SetRenderDrawColor(context, 40, 40, 40, 255);
		SDL_RenderClear(context);

		SDL_RenderCopy(context, background, 0, 0);

		SDL_SetRenderTarget(context, terrain);
		SDL_SetRenderDrawColor(context, 0, 0, 0, 0);
		SDL_RenderClear(context);
		SDL_RenderCopy(context, mask_texture, 0, 0);
		SDL_RenderCopy(context, foreground, 0, 0);
		SDL_SetRenderTarget(context, 0);
		SDL_RenderCopy(context, terrain, 0, 0);

		SDL_SetRenderDrawColor(context, 255, 0, 0, 255);
		SDL_Rect player_rect = {player.x - 5, WINDOW_H - player.y - 10, 10, 10 };
		SDL_RenderFillRect(context, &player_rect);

		SDL_RenderDrawLine(context, player.x, WINDOW_H - player.y, player.x + cos(player.angle) * 100, WINDOW_H - player.y - sin(player.angle) * 100);

		if (projectile.alive) {
			SDL_SetRenderDrawColor(context, 0, 255, 0, 255);
			SDL_Rect projectile_rect = {projectile.x - 5, WINDOW_H - projectile.y - 10, 10, 10 };
			SDL_RenderFillRect(context, &projectile_rect);
		}

		SDL_RenderPresent(context);
		SDL_Delay(1);
	}
}

int main() {
	SDL_Init(SDL_INIT_VIDEO);

	SDL_Window *window = SDL_CreateWindow("Cannons",
			SDL_WINDOWPOS_UNDEFINED,
			SDL_WINDOWPOS_UNDEFINED,
			WINDOW_W,
			WINDOW_H,
			0);

	SDL_Renderer *context = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);

	GameMain(context);

	SDL_DestroyRenderer(context);
	SDL_DestroyWindow(window);
	SDL_Quit();
	return 0;
}