-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.cpp
executable file
·120 lines (103 loc) · 2.37 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#include "headers/window.h"
int Window::height;
int Window::width;
Window::Window()
{
Init();
Window::height = 640;
Window::width = 400;
mCamera = {0,0,640,480};
TextureLoader::Instance()->SetWindow(this);
}
Window::~Window()
{
SDL_DestroyRenderer( mRenderer );
SDL_DestroyWindow( mWindow );
mWindow = NULL;
mRenderer = NULL;
//Quit SDL subsystems
IMG_Quit();
SDL_Quit();
}
bool Window::Init()
{
bool success = true;
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
printf( "SDL could not initialize! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Set texture filtering to linear
if( !SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" ) )
{
printf( "Warning: Linear texture filtering not enabled!" );
}
//Create window
mWindow = SDL_CreateWindow( "First Window", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, mWidth, mHeight, SDL_WINDOW_SHOWN );
if( mWindow == NULL )
{
printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Create renderer for window
mRenderer = SDL_CreateRenderer( mWindow, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
if( mRenderer == NULL )
{
printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
success = false;
}
else
{
//Initialize renderer color
SDL_SetRenderDrawColor( mRenderer, 0x66, 0xFF, 0xFF, 0xFF );
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if( !( IMG_Init( imgFlags ) & imgFlags ) )
{
printf( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
success = false;
}
//Initialize SDL_ttf
if( TTF_Init() == -1 )
{
printf( "SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError() );
success = false;
}
}
}
}
return success;
}
SDL_Window* Window::GetWindow() const
{
return mWindow;
}
SDL_Rect* Window::GetCamera()
{
return &mCamera;
}
void Window::SetCamera(int x,int y, int w, int h)
{
mCamera = {x,y,w,h};
}
SDL_Renderer* Window::GetRenderer() const
{
return mRenderer;
}
int Window::GetWidth()
{
int x,y;
SDL_GetWindowSize(mWindow,&x,&y);
return x;
}
int Window::GetHeight()
{
int x,y;
SDL_GetWindowSize(mWindow,&x,&y);
return y;
}