Added basic Window structure

This commit is contained in:
Robert 2020-11-05 14:58:07 +01:00
parent 2f42093946
commit 65e5993ee4
4 changed files with 70 additions and 30 deletions

17
include/RenderWindow.hpp Normal file
View file

@ -0,0 +1,17 @@
#pragma once
struct SDL_Window;
struct SDL_Renderer;
class RenderWindow
{
public:
RenderWindow(int width, int height);
~RenderWindow();
void Run();
private:
SDL_Window* window;
SDL_Renderer* renderer;
};

View file

@ -1,6 +1,7 @@
add_executable(fourier
"main.cpp"
)
"RenderWindow.cpp"
)
target_include_directories(fourier PRIVATE
${CMAKE_SOURCE_DIR}/include

42
src/RenderWindow.cpp Normal file
View file

@ -0,0 +1,42 @@
#include "RenderWindow.hpp"
#include <SDL.h>
#include <stdexcept>
RenderWindow::RenderWindow(int width, int height)
{
if (SDL_CreateWindowAndRenderer(width, height, SDL_WINDOW_SHOWN, &window, &renderer))
{
throw std::runtime_error(SDL_GetErrorMsg((char*)malloc(512), 511));
}
}
RenderWindow::~RenderWindow()
{
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
}
void RenderWindow::Run()
{
SDL_Event event;
bool isOpen = true;
while (isOpen)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_WINDOWEVENT)
{
switch (event.window.event)
{
case SDL_WINDOWEVENT_CLOSE: isOpen = false; break;
}
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
}

View file

@ -1,40 +1,20 @@
#include <iostream>
#include <SDL.h>
#include "RenderWindow.hpp"
int main(int argc, char** argv)
{
SDL_Window* window;
SDL_Renderer* renderer;
if (SDL_CreateWindowAndRenderer(800, 800, SDL_WINDOW_SHOWN, &window, &renderer))
RenderWindow* window;
try
{
std::cerr << "Failed to initialize Window and Renderer" << std::endl;
window = new RenderWindow(800, 800);
}
catch (const std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
return -1;
}
SDL_Event event;
bool isOpen = true;
while (isOpen)
{
while (SDL_PollEvent(&event))
{
if (event.type == SDL_WINDOWEVENT)
{
switch (event.window.event)
{
case SDL_WINDOWEVENT_CLOSE: isOpen = false; break;
}
}
}
SDL_SetRenderDrawColor(renderer, 20, 40, 210, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);
}
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
window->Run();
return 0;
}