simulation/Window.cpp

85 lines
1.8 KiB
C++
Raw Normal View History

2022-10-30 23:44:30 +01:00
#include "Window.h"
#include "ErrorHandler.h"
#include <SDL2/SDL.h>
2022-11-02 07:44:57 +01:00
#include <SDL2/SDL_rect.h>
#include <SDL2/SDL_render.h>
#include <SDL2/SDL_video.h>
2022-10-30 23:44:30 +01:00
#include <iostream>
2022-11-02 07:44:57 +01:00
#include <vector>
2022-10-30 23:44:30 +01:00
2022-11-02 07:44:57 +01:00
void Window::Draw(std::function<bool(Window*)> setup, std::function<bool(Window*)> draw){
setup(this);
2022-10-30 23:44:30 +01:00
while(Running){
2022-11-02 07:44:57 +01:00
SDL_SetRenderDrawColor(ren, 0x00,0x00,0x00,0xff);
2022-10-30 23:44:30 +01:00
SDL_RenderClear(ren);
2022-11-02 07:44:57 +01:00
Events(draw);
draw(this);
2022-10-30 23:44:30 +01:00
SDL_RenderPresent(ren);
}
}
2022-11-02 07:44:57 +01:00
void Window::Events(std::function<bool(Window*)> draw){
while(SDL_PollEvent(&e)){
if(e.type == SDL_QUIT){
2022-10-30 23:44:30 +01:00
Running = false;
}
2022-11-02 07:44:57 +01:00
if(e.type == SDL_KEYDOWN){
switch(e.key.keysym.sym){
2022-10-30 23:44:30 +01:00
case SDLK_a:
Running = false;
break;
2022-11-02 07:44:57 +01:00
case SDLK_s:
step = true;
break;
2022-10-30 23:44:30 +01:00
}
}
}
}
Window::Window(std::string title, int width, int height)
: Running(true)
{
if(SDL_Init(SDL_INIT_VIDEO) != 0){
ErrorHandler::send(ErrorType::SDL_ERROR, "SDL_Init");
}
2022-11-02 07:44:57 +01:00
win = SDL_CreateWindow("test", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN | SDL_WINDOW_FULLSCREEN_DESKTOP);
2022-10-30 23:44:30 +01:00
if(win == NULL){
ErrorHandler::send(ErrorType::SDL_ERROR, "SDL_CreateWindow");
}
ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_SOFTWARE);
if(ren == NULL){
ErrorHandler::send(ErrorType::SDL_ERROR, "SDL_CreateRenderer");
}
}
Window::~Window(){
SDL_DestroyWindow(win);
SDL_DestroyRenderer(ren);
SDL_Quit();
}
SDL_Renderer* Window::GetRenderer(){
return ren;
}
2022-11-02 07:44:57 +01:00
void Window::DrawCircle(SDL_Renderer* ren, int x, int y, int r, bool filled){
std::vector<SDL_Point> circle;
for(int _x = x - r; _x <= x + r; _x++){
for(int _y = y - r; _y <= y + r; _y++){
int dx = _x - x;
int dy = _y - y;
if (dx*dx + dy*dy <= r*r){
circle.push_back({_x, _y});
}
}
}
SDL_RenderDrawPoints(ren, circle.data(), circle.size());
}