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 <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#define WINDOW_W 1280
#define WINDOW_H 720
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);
Uint32 pixelformat;
SDL_QueryTexture(texture_tile, &pixelformat, 0, 0, 0);
SDL_Texture *texture = SDL_CreateTexture(context, pixelformat, SDL_TEXTUREACCESS_STATIC | 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_SetRenderDrawColor(context, 40, 40, 40, 255);
SDL_RenderClear(context);
SDL_Texture *background = createTiledTexture(context, "background.bmp");
SDL_Texture *foreground = createTiledTexture(context, "foreground.bmp");
// TODO:
// - Map has a mask generated by the game, of where the terrain (foregroud) exists
float *mask = calloc(WINDOW_W * WINDOW_H, 4);
int exit = 0;
while (!exit) {
SDL_Event event = {0};
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
exit = 1;
}
}
SDL_RenderCopy(context, background, 0, 0);
SDL_SetRenderTarget(context, foreground);
// TODO: Get masking right
SDL_SetTextureBlendMode(mask_texture, SDL_BLENDMODE_MUL);
SDL_RenderCopy(context, mask_texture, 0, 0);
SDL_SetRenderTarget(context, 0);
SDL_RenderPresent(context);
SDL_Delay(10);
}
SDL_DestroyRenderer(context);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
|