summaryrefslogtreecommitdiff
path: root/main.c
blob: da37ce4e4e75b48cb8cb6cfbdc526dd2851b384d (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
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>

#define WINDOW_W 1280
#define WINDOW_H 720

void renderMaskedTexture(SDL_Renderer *context, SDL_Texture *result, SDL_Texture *mask, SDL_Texture *diffuse) {
	SDL_SetRenderTarget(context, result);
	SDL_SetRenderDrawColor(context, 0, 0, 0, 0);
	SDL_RenderClear(context);
	SDL_RenderCopy(context, mask, 0, 0);
	SDL_RenderCopy(context, diffuse, 0, 0);
	SDL_SetRenderTarget(context, 0);
}

SDL_Texture *createTiledTexture(SDL_Renderer *context, const char *filename) {
	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,
			WINDOW_W, WINDOW_H);
	SDL_SetRenderTarget(context, texture);
	for (int y = 0; y < WINDOW_H; y += tile_h) {
		for (int x = 0; x < WINDOW_W; 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;
}

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);

	SDL_Texture *background = createTiledTexture(context, "background.bmp");

	SDL_Texture *foreground = createTiledTexture(context, "foreground.bmp");
	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);

	// Terrain generation
	SDL_Surface *mask_surface = SDL_CreateRGBSurfaceWithFormat(0,
			WINDOW_W, WINDOW_H, 32, SDL_PIXELFORMAT_RGBA8888);
	SDL_LockSurface(mask_surface);
	for (int i = 0; i < mask_surface->w * mask_surface->h; i++) {
		((Uint32 *)mask_surface->pixels)[i] = 0x00000000;
	}
	for (int i = mask_surface->w * 500; i < mask_surface->w * mask_surface->h; i++) {
		((Uint32 *)mask_surface->pixels)[i] = 0xffffffff;
	}
	SDL_UnlockSurface(mask_surface);
	SDL_Texture *mask_texture = SDL_CreateTextureFromSurface(context, mask_surface);
	renderMaskedTexture(context, terrain, mask_texture, foreground);

	// Player variables
	float player_x = 100;
	float player_y = 100;
	float player_vy = 0;
	float player_ay = 1;

	// Timer variables
	float old_t = 0.0;

	// Event handling
	int exit = 0;
	while (!exit) {
		SDL_Event event = {0};
		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);
		if (mouse_state & SDL_BUTTON_LMASK) {
			SDL_LockSurface(mask_surface);
			unsigned size = 100;
			for (int j = mouse_y - size / 2; j < mouse_y + size / 2; j++) {
				for (int i = mouse_x - size / 2; i < mouse_x + size / 2; i++) {
					size_t index = i + j * WINDOW_W;
					((Uint32 *)mask_surface->pixels)[index] = 0xffffffff;
				}
			}
			SDL_UnlockSurface(mask_surface);
			SDL_DestroyTexture(mask_texture);
			mask_texture = SDL_CreateTextureFromSurface(context, mask_surface);

			renderMaskedTexture(context, terrain, mask_texture, foreground);
		} else if (mouse_state & SDL_BUTTON_RMASK) {
			SDL_LockSurface(mask_surface);
			unsigned size = 100;
			for (int j = mouse_y - size / 2; j < mouse_y + size / 2; j++) {
				for (int i = mouse_x - size / 2; i < mouse_x + size / 2; i++) {
					size_t index = i + j * WINDOW_W;
					((Uint32 *)mask_surface->pixels)[index] = 0x00000000;
				}
			}
			SDL_UnlockSurface(mask_surface);
			SDL_DestroyTexture(mask_texture);
			mask_texture = SDL_CreateTextureFromSurface(context, mask_surface);

			renderMaskedTexture(context, terrain, mask_texture, foreground);
		}

		// Main loop
		SDL_SetRenderDrawColor(context, 40, 40, 40, 255);
		SDL_RenderClear(context);

		SDL_RenderCopy(context, background, 0, 0);
		SDL_RenderCopy(context, terrain, 0, 0);

		// Time delta calculation
		double new_t = (double) SDL_GetTicks64() / 1000.0;
		double dt = new_t - old_t;
		old_t = new_t;

		// Player logic
		player_y += player_vy * dt;
		player_vy += player_ay * dt;

		// Draw player
		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_RenderPresent(context);
		SDL_Delay(1);
	}

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