summaryrefslogtreecommitdiff
path: root/sdl3_main.c
blob: 701dfe4becadfc7be22327329ebea3620115d7c4 (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
#include <stdbool.h>
#include <stdio.h>

#include <SDL3/SDL.h>

#include "dat.h"

static void render(void);
static void update(float delta);
static void process_events(void);

static SDL_Window *window;
static SDL_Renderer *renderer;
static bool running;
static Uint64 old_ticks, new_ticks;

static float camera_x, camera_y;

bool shot;
bool moveup, movedown, moveleft, moveright;
int shotx, shoty;

int
main()
{
	if(!SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO)) {
		fprintf(stderr, "SDL_Init(): %s\n", SDL_GetError());
		return 1;
	}

	if(!SDL_CreateWindowAndRenderer("window", 800, 600, SDL_WINDOW_OPENGL, &window, &renderer)) {
		fprintf(stderr, "SDL_CreateWindowAndRenderer: %s\n", SDL_GetError());
		return -1;
	}

	entinit();
	running = true;
	old_ticks = SDL_GetTicksNS();

	while(running) {
		double delta;
		process_events();

		new_ticks = SDL_GetTicksNS();
	    delta = (new_ticks - old_ticks) / 1000000000.0;
		old_ticks = new_ticks;
		update(delta);

		render();
		SDL_RenderPresent(renderer);
	}
	
	SDL_DestroyWindow(window);
	SDL_Quit();
	return 0;
}

void
render(void)
{
	{
		float plx, ply, minx, miny, maxx, maxy;

		plx = player->body.pos[0] - camera_x;
		ply = player->body.pos[1] - camera_y;

		minx = plx - 80;
		miny = ply - 80;
		maxx = plx + 80;
		maxy = ply + 80;
		
		if(minx < 0)
			camera_x += minx;

		if(miny < 0)
			camera_y += miny;

		if(maxx > 800)
			camera_x += (maxx - 800);

		if(maxy > 600)
			camera_y += (maxy - 600);
	}

	SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0xFF);
	SDL_RenderClear(renderer);

	SDL_SetRenderDrawColor(renderer, 0xAA, 0xAA, 0xAA, 0xFF);
	for(int y = 0; y < map_height; y++)
		for(int x = 0; x < map_width; x++) {
			if(!map_data[x + map_width * y])
				continue;

			SDL_RenderFillRect(renderer, &(SDL_FRect) {
				.x = 2 * ENTITY_SIZE * x - ENTITY_SIZE - camera_x,
				.y = 2 * ENTITY_SIZE * y - ENTITY_SIZE - camera_y,
				.w = 2.0 * ENTITY_SIZE,
				.h = 2.0 * ENTITY_SIZE,
			});
		}

	for(int i = 0; i < ebufi; i++) {
		Entity *e = entitybuffer + i;
		if(!e->active)
			continue;

		switch (e->type) {
		case EPLAYER:
			SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);
			SDL_RenderRect(renderer, &(SDL_FRect){
					.x = e->body.pos[0] - ENTITY_SIZE - camera_x,
					.y = e->body.pos[1] - ENTITY_SIZE - camera_y, 
					.w = ENTITY_SIZE * 2.0,
					.h = ENTITY_SIZE * 2.0
				});
			break;

		case EGHOST:
			SDL_SetRenderDrawColor(renderer, 0xAA, 0xAA, 0xAA, 0xFF);
			SDL_RenderRect(renderer, &(SDL_FRect){
					.x = e->body.pos[0] - ENTITY_SIZE - camera_x,
					.y = e->body.pos[1] - ENTITY_SIZE - camera_y, 
					.w = ENTITY_SIZE * 2.0,
					.h = ENTITY_SIZE * 2.0
				});
			break;

		case EBULLET:
			SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0x00, 0xFF);
			SDL_RenderRect(renderer, &(SDL_FRect){
					.x = e->body.pos[0] - BULLET_SIZE - camera_x,
					.y = e->body.pos[1] - BULLET_SIZE - camera_y, 
					.w = BULLET_SIZE * 2.0,
					.h = BULLET_SIZE * 2.0
				});
			break;
		}
	}
}

void
update(float delta)
{
	entphysics(delta);
	entupdate(delta);
}

void
process_events()
{
	SDL_Event event;

	while(SDL_PollEvent(&event)) {
		switch(event.type) {
			case SDL_EVENT_QUIT:
				running = false;
				break;
			case SDL_EVENT_MOUSE_BUTTON_DOWN:
				shot = true;
				shotx = event.button.x + camera_x;
				shoty = event.button.y + camera_y;
				break;

			case SDL_EVENT_KEY_UP:
			case SDL_EVENT_KEY_DOWN:
				#define DEFINE_KEY(X, VAR) \
					if(event.key.scancode == X) VAR = event.type == SDL_EVENT_KEY_DOWN

				DEFINE_KEY(SDL_SCANCODE_W, moveup);
				DEFINE_KEY(SDL_SCANCODE_A, moveleft);
				DEFINE_KEY(SDL_SCANCODE_S, movedown);
				DEFINE_KEY(SDL_SCANCODE_D, moveright);

				#undef DEFINE_KEY

				break;
		}
	}
}