-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsdlframework.cpp
158 lines (139 loc) · 4.13 KB
/
sdlframework.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
/*
* File: sdlframework.cpp
* Authors: Alexander Epp, Rain Epp
* Project: CMPUT274 Final Project
* Description: Contains implementation of the SDLFramework class from
* sdlframework.h
*/
#ifndef ARDUINO_BUILD
#include "sdlframework.h"
#include "util.h"
#include "vec.h"
SDLFramework::SDLFramework()
// : m_width(320), m_height(240),
: m_width(1920), m_height(1080),
// : m_width(1920/2), m_height(1080/2),
m_updatePeriod(1000)
{
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
util::debugPrint("Cannot initialize SDL");
return;
}
uint32_t windowFlags = 0;
SDL_DisplayMode dm;
if (SDL_GetDesktopDisplayMode(0, &dm) != 0)
{
util::debugPrint("SDL display mode query failed");
return;
}
// If a window is being created with dimensions equal to screen dimensions,
// make that window full screen.
if (dm.w == m_width && dm.h == m_height)
{
windowFlags |= SDL_WINDOW_FULLSCREEN_DESKTOP;
}
// Create and display window
if (SDL_CreateWindowAndRenderer(m_width, m_height, windowFlags, &m_window, &m_renderer))
{
util::debugPrint("Cannot create SDL window and renderer.");
return;
}
// Create back buffer texture
m_backBuffer = SDL_CreateTexture(
m_renderer,
SDL_PIXELFORMAT_ARGB8888,
SDL_TEXTUREACCESS_STREAMING,
m_width,
m_height);
lockBackBuffer();
m_lastUpdate = SDL_GetTicks();
}
SDLFramework::~SDLFramework()
{
SDL_DestroyRenderer(m_renderer);
SDL_DestroyWindow(m_window);
SDL_Quit();
}
void SDLFramework::paintBackBuffer()
{
unlockBackBuffer();
SDL_RenderCopy(m_renderer, m_backBuffer, nullptr, nullptr);
SDL_RenderPresent(m_renderer);
lockBackBuffer();
}
void SDLFramework::unlockBackBuffer()
{
SDL_UnlockTexture(m_backBuffer);
}
void SDLFramework::lockBackBuffer()
{
SDL_LockTexture(m_backBuffer, nullptr, (void**)&m_backBufferPixels, &m_backBufferPitch);
}
bool SDLFramework::tick()
{
// Process all events that happened since this function was last called
while (SDL_PollEvent(&m_event))
{
if (m_event.type == SDL_QUIT) return false;
if (m_event.type == SDL_KEYUP && m_event.key.keysym.sym == SDLK_ESCAPE) return false;
}
auto currentTime = SDL_GetTicks();
auto elapsedTime = currentTime - m_lastUpdate;
if (elapsedTime >= m_updatePeriod)
{
// Periodically update screen with pixels from backbuffer.
paintBackBuffer();
m_lastUpdate = currentTime;
}
return true;
}
bool SDLFramework::idle()
{
// Draw all remaining pixels in backbuffer
paintBackBuffer();
// Wait for events (should probably just loop until quit, re-drawing after
// each event)
if (SDL_WaitEvent(&m_event) == 0)
{
util::debugPrint("SDL_WaitEvent failed.");
return false;
}
if (m_event.type == SDL_QUIT) return false;
if (m_event.type == SDL_KEYUP && m_event.key.keysym.sym == SDLK_ESCAPE) return false;
return true;
}
void SDLFramework::clear(fvec3 colour)
{
// Clear backbuffer to the given colour
for (int i = 0; i < m_width*m_height; ++i)
*(m_backBufferPixels+i) = encodeColour(colour);
// Update screen screen with backbuffer (not actually guaranteed by the
// WindowFramework interface, but no harm in doing it here since it is not
// prohibited either).
paintBackBuffer();
}
uint32_t SDLFramework::encodeColour(fvec3 colour)
{
int r = util::max(util::min(colour.r * 0xFF, 255.f), 0.f);
int g = util::max(util::min(colour.g * 0xFF, 255.f), 0.f);
int b = util::max(util::min(colour.b * 0xFF, 255.f), 0.f);
return 0xFF000000 | (r<<16) | (g<<8) | b;
}
void SDLFramework::drawPixel(uint16_t x, uint16_t y, fvec3 colour)
{
// Pixels are normally specified as x+y*width, but in this format y=0 is the
// top of the screen and y=height-1 is the bottom, and we want the opposite
// of that.
*(m_backBufferPixels+x + (m_height-1-y)*m_width) = encodeColour(colour);
}
uint16_t SDLFramework::width()
{
return m_width;
}
uint16_t SDLFramework::height()
{
return m_height;
}
#endif