/
SDLTest.cpp
198 lines (165 loc) · 4.42 KB
/
SDLTest.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
//
// SDLTest.cpp
// Created by Vraiment on September 7th, 2015
//
// File crated to verify an issue I'm having with SDL under windows.
// Through this file I use std::shared_ptr to automatically manage and destroy
// the different pointes created by SDL and make the code easier to read.
//
#include <iostream>
#include <memory>
#include <SDL.h>
#include <SDL_image.h>
#include <string>
using namespace std;
// Dimentions of the window
static const int WINDOW_WIDTH = 800;
static const int WINDOW_HEIGHT = 600;
// Simple class to initialize and close SDL
class SDL
{
public:
SDL();
~SDL();
bool isLoaded() const;
private:
bool loaded;
};
// Function to create a SDL window
shared_ptr<SDL_Window> createWindow();
// Function to create a SDL renderer from the given window
shared_ptr<SDL_Renderer> createRenderer(shared_ptr<SDL_Window> window);
// Function to create a texture with the given color and rederer
shared_ptr<SDL_Texture> createRGBTexture(
uint8_t r,
uint8_t g,
uint8_t b,
shared_ptr<SDL_Renderer> renderer);
int main(int argc, char *argv[])
{
// This line initializes SDL and makes sure that whenever
// we quit from the main function, SDL_Quit() gets called.
SDL sdl;
// Check if SDL was loaded
if (!sdl.isLoaded())
{
cerr << "Could not initialize SDL" << endl;
return 1;
}
// Create both window and renderer
auto window = createWindow();
auto renderer = createRenderer(window);
// Create a red texture
auto t1 = createRGBTexture(0xFF, 0x00, 0x00, renderer);
if (!t1)
{
cerr << "Could not create first texture" << endl;
return 1;
}
// Create a cyan texcture
auto t2 = createRGBTexture(0x00, 0xFF, 0xFF, renderer);
if (!t2)
{
cerr << "Could not create first texture" << endl;
return 1;
}
int ticks = SDL_GetTicks(), seconds = 0;
bool quit = false;
SDL_Event event;
while (!quit)
{
while (SDL_PollEvent(&event))
{
if (SDL_QUIT == event.type)
{
quit = true;
}
}
// With this code we get how many milliseconds have passed
// between each frame, for every second we print it to the
// standard output.
int currentTicks = SDL_GetTicks();
if ((currentTicks - ticks) >= 1000)
{
cout << seconds++ << endl;
ticks = currentTicks;
}
SDL_RenderClear(renderer.get());
// This is just a 'clever' way to switch textures for every
// second that has passed.
shared_ptr<SDL_Texture> t = (seconds & 1) ? t1 : t2;
SDL_RenderCopy(renderer.get(), t.get(), nullptr, nullptr);
SDL_RenderPresent(renderer.get());
}
// We don't have to do any clean up, everything is done automatically.
return 0;
}
SDL::SDL() : loaded(false)
{
loaded = !SDL_Init(SDL_INIT_EVERYTHING);
}
SDL::~SDL()
{
if (loaded)
{
SDL_Quit();
}
}
bool SDL::isLoaded() const
{
return loaded;
}
shared_ptr<SDL_Window> createWindow()
{
shared_ptr<SDL_Window> result;
SDL_Window *window;
window = SDL_CreateWindow(
"Test",
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
WINDOW_WIDTH,
WINDOW_HEIGHT,
SDL_WINDOW_SHOWN);
if (nullptr != window)
{
result.reset(window, SDL_DestroyWindow);
}
return result;
}
shared_ptr<SDL_Renderer> createRenderer(shared_ptr<SDL_Window> window)
{
shared_ptr<SDL_Renderer> result;
SDL_Renderer *renderer;
renderer = SDL_CreateRenderer(window.get(), -1, SDL_RENDERER_ACCELERATED);
if (nullptr != renderer)
{
result.reset(renderer, SDL_DestroyRenderer);
}
return result;
}
shared_ptr<SDL_Texture> createRGBTexture(
uint8_t r,
uint8_t g,
uint8_t b,
shared_ptr<SDL_Renderer> renderer)
{
shared_ptr<SDL_Texture> result;
SDL_Surface *surface = SDL_CreateRGBSurface(
0,
WINDOW_WIDTH,
WINDOW_HEIGHT,
32, 0, 0, 0, 0);
if (nullptr != surface)
{
uint32_t rgbMap = SDL_MapRGB(surface->format, r, g, b);
if (!SDL_FillRect(surface, nullptr, rgbMap))
{
SDL_Texture *t = SDL_CreateTextureFromSurface(renderer.get(), surface);
if (nullptr != t)
{
result.reset(t, SDL_DestroyTexture);
}
}
}
return result;
}