summaryrefslogtreecommitdiff
path: root/platform.c
blob: 7ba6d53ff82dbd862da51f5b244d17f40275bf1a (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
#include <SDL2/SDL.h>

#define WINDOW_W 1280
#define WINDOW_H 720

SDL_Renderer *context = 0;
SDL_Texture *background = 0;
SDL_Texture *foreground = 0;
SDL_Texture *terrain = 0;
SDL_Texture *mask_texture = 0;

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 PlatformMain(void (*GameMain)(void)) {
	SDL_Init(SDL_INIT_VIDEO);

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

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

	background = createTiledTexture(context, "background.bmp", WINDOW_W, WINDOW_H);

	foreground = createTiledTexture(context, "foreground.bmp", WINDOW_W, WINDOW_H);
	SDL_SetTextureBlendMode(foreground, SDL_BLENDMODE_MOD);

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

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

	GameMain();

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

void RenderScene(float *mask, int player_x, int player_y) {
	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_RenderPresent(context);
}