aboutsummaryrefslogtreecommitdiff
path: root/src/crepe/SDLApp.cpp
blob: 198c0d002924d579c94382df13aee97ce326bc02 (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
#include "SDLApp.h"
#include "Particle.h"
#include "api/ParticleEmitter.h"
#include <iostream>
#include <vector>

SDLApp::SDLApp(int window_width, int window_height)
	: window_width(window_width), window_height(window_height), window(nullptr),
	  renderer(nullptr) {}

SDLApp::~SDLApp() { clean_up(); }

bool SDLApp::initialize() {
	if (SDL_Init(SDL_INIT_VIDEO) != 0) {
		std::cerr << "SDL Initialization Error: " << SDL_GetError()
				  << std::endl;
		return false;
	}

	window = SDL_CreateWindow("Particle System", SDL_WINDOWPOS_CENTERED,
							  SDL_WINDOWPOS_CENTERED, window_width, window_height,
							  SDL_WINDOW_SHOWN);
	if (!window) {
		std::cerr << "Window Creation Error: " << SDL_GetError() << std::endl;
		return false;
	}

	renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
	if (!renderer) {
		std::cerr << "Renderer Creation Error: " << SDL_GetError() << std::endl;
		return false;
	}

	return true;
}

void SDLApp::handle_events(bool & running) {
	SDL_Event event;
	while (SDL_PollEvent(&event)) {
		if (event.type == SDL_QUIT) {
			running = false;
		}
	}
}

void SDLApp::clear_screen() {
	SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
	SDL_RenderClear(renderer);
}

void SDLApp::present_screen() { SDL_RenderPresent(renderer); }

void SDLApp::draw_square(int x, int y, int size) {
	SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
	SDL_Rect rect = {x, y, size, size};
	SDL_RenderFillRect(renderer, &rect);
}

SDL_Texture * square_texture = nullptr; // Load this with an image or create it

void SDLApp::clean_up() {
	if (renderer) {
		SDL_DestroyRenderer(renderer);
	}
	if (window) {
		SDL_DestroyWindow(window);
	}
	SDL_Quit();
}