-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWindow.cpp
76 lines (63 loc) · 1.51 KB
/
Window.cpp
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
//
// Created by vincenzo on 30/09/22.
//
#include "Window.h"
#include <exception>
Window* Window::instance= nullptr;
Window::Window(){
if(SDL_Init(SDL_INIT_VIDEO) != 0){
SDL_Log("Unable to initialize SDL: %s", SDL_GetError());
exit(1);
}
sdlWindow = SDL_CreateWindow(title.c_str(),SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,
width,height,SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE);
if(sdlWindow){
sdlRenderer= SDL_CreateRenderer(sdlWindow,-1,SDL_RENDERER_ACCELERATED );
if(sdlRenderer){
running= true;
}else{
SDL_Log("Unable to create 2d rendering context: %s", SDL_GetError());
}
}else{
running= false;
}
}
Window *Window::getInstance() {
if(instance == nullptr){
instance=new Window();
}
return instance;
}
Window::~Window() {
//Free sdl2 resources
SDL_DestroyRenderer(sdlRenderer);
SDL_DestroyWindow(sdlWindow);
}
SDL_Renderer *Window::getRenderer() {
return sdlRenderer;
}
void Window::quit() {
running = false;
}
bool Window::isRunning() const {
return running;
}
void Window::events(SDL_Event& e) {
switch (e.type) {
case SDL_QUIT:
quit();
}
}
int Window::getWidth() const {
int w;
SDL_GetWindowSize(sdlWindow,&w, nullptr);
return w;
}
int Window::getHeight() const {
int h;
SDL_GetWindowSize(sdlWindow, nullptr,&h);
return h;
}
SDL_Window *Window::getSdlWindow() {
return sdlWindow;
}