diff options
author | Matt Kohls <mattkohls13@gmail.com> | 2019-01-15 22:51:01 -0500 |
---|---|---|
committer | Matt Kohls <mattkohls13@gmail.com> | 2019-01-15 22:51:01 -0500 |
commit | 5d8f5c1499f4cc42a1dce5cb4a1775cccf0da0f1 (patch) | |
tree | 498495ce8d2846143755a27bf846af5396888ef9 /sdlwindow.h | |
parent | 231e342f52ca6ce9864c66e00f53bb4de568f93f (diff) | |
download | fungi-refactor.tar.gz fungi-refactor.tar.bz2 fungi-refactor.zip |
Breaking things out into different filesrefactor
Diffstat (limited to 'sdlwindow.h')
-rw-r--r-- | sdlwindow.h | 143 |
1 files changed, 143 insertions, 0 deletions
diff --git a/sdlwindow.h b/sdlwindow.h new file mode 100644 index 0000000..ade4e3f --- /dev/null +++ b/sdlwindow.h @@ -0,0 +1,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 |