aboutsummaryrefslogtreecommitdiffstats
path: root/sdlwindow.h
blob: ade4e3f315f928f5e6a1fcac0007ec39b06e45cd (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
/**
 *
 *
 */

#ifndef SDLWINDOW_H
#define SDLWINDOW_H

#include "window.h"
#include <iostream>
#include <SDL2/SDL.h>

/*
 * SDLWindow
 * The display into our fungus world
 */
class SDLWindow : public Window {
private:
	SDL_Window* sdlWindow;
	SDL_Renderer* sdlRenderer;

	/**
	 * Log an SDL error with some error message to the output stream of our choice
	 * @param os The output stream to write the message to
	 * @param msg The error message to write, format will be msg error: SDL_GetError()
	 */
	void logSDLError(std::ostream &os, const std::string &msg) {
		os << msg << " error: " << SDL_GetError() << std::endl;
	}

	/**
	 * Brings up all the SDL objects we will need to draw our window
	 * @param winpos The position we want for the window
	 * @param winsize The size we want for the window
	 */
	void init() {
		if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
			logSDLError(std::cout, "SDL_Init");
			bad = true;
		}
		sdlWindow = SDL_CreateWindow("Fungi", winposition.posx, 
										winposition.posy,
										winsize.width, winsize.height,
										SDL_WINDOW_OPENGL);
		if (sdlWindow == nullptr){
			logSDLError(std::cout, "CreateWindow");
			SDL_Quit();
			bad = true;
		}
		sdlRenderer = SDL_CreateRenderer(sdlWindow, -1,
											SDL_RENDERER_SOFTWARE);
		if (sdlRenderer == nullptr){
			logSDLError(std::cout, "CreateRenderer");
			SDL_DestroyWindow(sdlWindow);
			sdlWindow = nullptr;
			SDL_Quit();
			bad = true;
		}
		SDL_SetRenderDrawColor(sdlRenderer, 0, 0, 0, 255);
		SDL_RenderClear(sdlRenderer);
		SDL_RenderPresent(sdlRenderer);
	}

	/**
	 * Draws a fungus on the screen
	 * @param f The fungus we want to draw
	 */
	void drawFungus(const Fungus& f) {
		Color color = f.getColor();
		Location loc = f.getLocation();
		SDL_SetRenderDrawColor(sdlRenderer, color.red, color.green, color.blue,
								color.alpha);
		SDL_RenderDrawPoint(sdlRenderer, loc.posx, loc.posy);
	}

public:
	/**
	 * Creates a SDLWindow without anything to show
	 */
	SDLWindow() : Window() {
		sdlWindow = nullptr;
		sdlRenderer = nullptr;
	}

	/**
	 * Creates a SDLWindow with an attached Petri dish
	 * @param p A petri dish
	 */
	SDLWindow(const Window_Pos& pos, const Window_Size& size, const Petri& p) :
		Window(pos, size, p) {
		init();
	}

	/**
	 * Destroyer of the window
	 */
	~SDLWindow() {
		if(sdlWindow != nullptr) {
			SDL_DestroyWindow(sdlWindow);
		}
		if(sdlRenderer != nullptr) {
			SDL_DestroyRenderer(sdlRenderer);
		}
		SDL_Quit();
	}

	/**
	 * Runs the growth of the fungus till the end of time
	 */
	void doFungus() {

		SDL_Event event;
		bool end = false;
		while(!end) {
			while(SDL_PollEvent(&event)) {
				if(event.type == SDL_QUIT) {
					end = true;
				}
			}

			for(int i = 0; i < petri.getSize(); i++) {
				if(petri[i] != nullptr) {
					drawFungus(*petri[i]);
				}
			}

			SDL_RenderPresent(sdlRenderer);

			petri.grow();
	    
			//SDL_Delay(TICKS);
		}
    
	}

	/**
	 * Tells if the windows is bad, aka an SDL error
	 * @return True if window is bad, False if not
	 */
	bool isBad() const { return bad; }
};

#endif