-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreen.cpp
161 lines (142 loc) · 3.42 KB
/
Screen.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
#include "Screen.h"
#include "App.h"
Screen::Screen(std::string bgnd): buttonMan()
{
background = SDL_LoadBMP(bgnd.c_str());
_running = false;
}
Screen::~Screen()
{
if (background) SDL_FreeSurface(background);
Destroy();
}
void Screen::DrawPixel(int x, int y, Uint8 R, Uint8 G, Uint8 B)
{
Uint32 pixel = SDL_MapRGB(background->format, R, G, B);
int bpp = background->format->BytesPerPixel;
// Here p is the address to the pixel we want to set
Uint8 *p = (Uint8 *)background->pixels + y * background->pitch + x * bpp;
switch(bpp)
{
case 1:
*p = pixel;
break;
case 2:
*(Uint16 *)p = pixel;
break;
case 3:
if (SDL_BYTEORDER == SDL_BIG_ENDIAN)
{
p[0] = (pixel >> 16) & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = pixel & 0xff;
}
else
{
p[0] = pixel & 0xff;
p[1] = (pixel >> 8) & 0xff;
p[2] = (pixel >> 16) & 0xff;
}
break;
case 4:
*(Uint32 *)p = pixel;
break;
}
}
void Screen::Add(Surface* item)
{
surfaces.push_back(item);
if ((item->GetName() & 0x80))
buttonMan.Add(dynamic_cast<Button*>(item));
}
void Screen::Destroy()
{
if (IsRunning() and ((bool)surfaces.empty() == false))
{
for (Uint32 i = 0; i < (Uint32)surfaces.size(); i++)
{
delete(surfaces[i]);
}
surfaces.clear();
buttonMan.Clear();
}
SetRunning(false);
}
void Screen::Clear()
{
SDL_BlitSurface(background, NULL, App::GetInstance()->screen, NULL);
}
bool Screen::IsRunning()
{
return _running;
}
void Screen::SetRunning(bool running)
{
_running = running;
}
void Screen::Draw()
{
for (Uint16 i = 0; i < (Uint16) surfaces.size(); i++)
{
/*
Surface* currSurface = surfaces[i];
if (currSurface->HasChanged()) // if the surface has changed
{
SDL_Rect clearArea = {currSurface->GetLastX(), currSurface->GetLastY(), currSurface->GetW(), currSurface->GetH()}; // the area that needs to be drawn over to cover the old object
SDL_BlitSurface(background, &clearArea, App::GetInstance()->screen, &clearArea); // cover the old object
currSurface->Draw(); // and draw the new one
//currSurface->SetChanged(false); // so the object wont keep drawing itself (this should be set back to true somewhere in Render() or HandleEvent())
}
*/
surfaces[i]->Draw();
}
}
void Screen::DrawAll()
{
for (Uint16 i = 0; i < (Uint16) surfaces.size(); i++)
{
surfaces[i]->Draw();
}
}
void Screen::Render()
{
for (Uint16 i = 0; i < (Uint16) surfaces.size(); i++)
{
Surface* currSurface = surfaces[i];
currSurface->Render();
}
}
void Screen::Events(SDL_Event* event)
{
for (Uint16 i = 0; i < (Uint16) surfaces.size(); i++)
{
surfaces[i]->HandleEvent(event);
}
}
SDL_Surface* Screen::Background()
{
return background;
}
void Screen::DrawRect(SDL_Rect* rect, Uint32 color)
{
SDL_FillRect(App::GetInstance()->screen, rect, color);
}
void Screen::DrawRect(Sint16 x, Sint16 y, Uint16 w, Uint16 h, Uint8 R, Uint8 G, Uint8 B)
{
Uint32 color = SDL_MapRGB(background->format, R, G, B);
SDL_Rect rect = {x,y,w,h};
SDL_FillRect(App::GetInstance()->screen, &rect, color);
}
void Screen::DrawRect(Sint16 x, Sint16 y, Uint16 w, Uint16 h, Uint32 color)
{
SDL_Rect rect = {x,y,w,h};
SDL_FillRect(App::GetInstance()->screen, &rect, color);
}
void Screen::PlaceSurface(SDL_Surface* surface, SDL_Rect* srcrect, SDL_Rect* destrect)
{
SDL_BlitSurface(surface, srcrect, App::GetInstance()->screen, destrect);
}
void Screen::ButtonCheck(SDL_Event* event)
{
buttonMan.CheckButtons(event);
}