Added basic Window structure
This commit is contained in:
parent
2f42093946
commit
65e5993ee4
17
include/RenderWindow.hpp
Normal file
17
include/RenderWindow.hpp
Normal 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;
|
||||
};
|
|
@ -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
42
src/RenderWindow.cpp
Normal 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);
|
||||
}
|
||||
}
|
38
src/main.cpp
38
src/main.cpp
|
@ -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;
|
||||
}
|
Loading…
Reference in a new issue