-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStDrawLogic.cpp
169 lines (137 loc) · 6.01 KB
/
StDrawLogic.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
#include "StDrawLogic.h"
StDrawLogic::StDrawLogic(const std::string * imagepath) {
m_openGL_texture_id = 1;
//ñîçäà¸ì îêíî
InitVideoOutput();
//çàãðóæàåì òàéëñåò
m_tilesetTexture = this->LoadTilesetTexture(imagepath);
}
void StDrawLogic::InitVideoOutput() {
m_screenSize.w = 800;
m_screenSize.h = 600;
if (SDL_Init(SDL_INIT_VIDEO) != 0){
std::cout << "Error: Could not init SDL library! Error: " << SDL_GetError() << std::endl;
exit(1);
}
m_window = SDL_CreateWindow("SDL2 Example", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, m_screenSize.w, m_screenSize.h, SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
if (m_window == nullptr){
std::cout << "Error: Could not create window! Error: " << SDL_GetError() << std::endl;
exit(1);
}
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetSwapInterval(1);
m_context = SDL_GL_CreateContext(m_window);
int res = SDL_GL_MakeCurrent(m_window, m_context);
if (res != 0) {
SDL_DestroyWindow(m_window);
exit(1);
}
if (SDL_GL_SetSwapInterval(1) < 0) {
std::cout << "Warning: Unable to set vsync! SDL error: " << SDL_GetError() << "\n";
}
//Initialize PNG loading
int imgFlags = IMG_INIT_PNG;
if( !( IMG_Init( imgFlags ) & imgFlags ) )
std::cout << "SDL_image could not initialize! SDL_image Error: " << IMG_GetError();
glViewport( 0, 0, m_screenSize.w, m_screenSize.h );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, m_screenSize.w, 0, m_screenSize.h, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
}
GLuint StDrawLogic::LoadTilesetTexture(const std::string * tileset_path) {
SDL_Surface *loadedSurface = IMG_Load(tileset_path->c_str());
if (loadedSurface == nullptr){
std::cout << "Warning: IMG_Load Error: " << SDL_GetError() << std::endl;
return 0;
}
SDL_Surface *gScreenSurface = SDL_GetWindowSurface(m_window);
SDL_Surface *optimizedSurface = SDL_ConvertSurface( loadedSurface, gScreenSurface->format, NULL );
if( optimizedSurface == NULL ) {
std::cout << "Warning: Unable to optimize image " << tileset_path << " SDL Error: " << SDL_GetError() << "\n";
optimizedSurface = loadedSurface;
}
GLuint resTexture;
glGenTextures(m_openGL_texture_id++,&resTexture);
glBindTexture(GL_TEXTURE_2D,resTexture);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, optimizedSurface->w, optimizedSurface->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, optimizedSurface->pixels);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
SDL_FreeSurface(loadedSurface);
SDL_FreeSurface(optimizedSurface);
return resTexture;
}
void StDrawLogic::DrawSprite(Sprite * pSpriteToDraw, const SDL_Rect & topLeftViewport) {
//åñëè ñïðàéò äëÿ îòðèñîâêè íå ïîïàäàåò â îáëàñòü ýêðàíà , òî ñêèïàåì åãî
if (pSpriteToDraw->getSurfaceDstRect().x < topLeftViewport.x ||
pSpriteToDraw->getSurfaceDstRect().x > (topLeftViewport.w + topLeftViewport.x))
return;
//óâåëè÷èâàåì ðàçìåð òàéëà ïðè êîïèðîâàíèè â 4 ðàçà
SDL_Rect window_dst_rect;
memcpy(&window_dst_rect, &pSpriteToDraw->getSurfaceDstRect(), sizeof(SDL_Rect));
window_dst_rect.x = (window_dst_rect.x - topLeftViewport.x) * 4;
window_dst_rect.y *= 4;
window_dst_rect.w *= 4;
window_dst_rect.h *= 4;
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glBindTexture(GL_TEXTURE_2D, m_tilesetTexture);
glBegin( GL_QUADS );
glTexCoord2f(pSpriteToDraw->getSurfaceSrcRect().x1, pSpriteToDraw->getSurfaceSrcRect().y1);
glVertex3f( window_dst_rect.x, m_screenSize.h - window_dst_rect.y, 0.0f );
glTexCoord2f(pSpriteToDraw->getSurfaceSrcRect().x2, pSpriteToDraw->getSurfaceSrcRect().y2);
glVertex3f( window_dst_rect.x + window_dst_rect.w, m_screenSize.h - window_dst_rect.y, 0.0f );
glTexCoord2f(pSpriteToDraw->getSurfaceSrcRect().x3, pSpriteToDraw->getSurfaceSrcRect().y3);
glVertex3f( window_dst_rect.x + window_dst_rect.w, m_screenSize.h - (window_dst_rect.y + window_dst_rect.h), 0.0f );
glTexCoord2f(pSpriteToDraw->getSurfaceSrcRect().x4, pSpriteToDraw->getSurfaceSrcRect().y4);
glVertex3f( window_dst_rect.x, m_screenSize.h - (window_dst_rect.y + window_dst_rect.h), 0.0f );
glEnd();
glLoadIdentity();
glDisable(GL_TEXTURE_2D);
}
void StDrawLogic::DrawTiles(std::unique_ptr<stSharedRes> & resource) {
glClearColor ( 1.0, 1.0, 1.0, 0.0 );
glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
//âû÷èñëÿåì îáëàñòü ðèñîâàíèÿ íà îñíîâàíèè ïîëîæåíèÿ èãðîêà
SDL_Rect topLeftViewport;
topLeftViewport.y = 0;
topLeftViewport.w = m_screenSize.w / 4;
topLeftViewport.h = m_screenSize.h / 4;
if (resource->playerBody->GetPosition().x <= m_screenSize.w/8)
topLeftViewport.x = 0;
else if (resource->playerBody->GetPosition().x > m_worldSizePixels.w - m_screenSize.w/8)
topLeftViewport.x = m_worldSizePixels.w - m_screenSize.w/4;
else
topLeftViewport.x = resource->playerBody->GetPosition().x - m_screenSize.w/8;
//ðèñóåì èãðîêà
DrawSprite(&resource->player.sprite, topLeftViewport);
//ðèñóåì ìîíåòêè
for(unsigned i = 0; i < resource->coin.size(); i++)
DrawSprite(&resource->coin[i].sprite, topLeftViewport);
//ðèñóåì âðàãîâ
for(unsigned i = 0; i < resource->enemy.size(); i++)
DrawSprite(&resource->enemy[i].sprite, topLeftViewport);
SDL_GL_SwapWindow(m_window);
}
void StDrawLogic::DrawBackground() {
}
void StDrawLogic::PrepareBackground(const Level * lvl)
{
//ïîëó÷àåì ðàçìåð èãðîâîãî ìèðà â ïèêñåëÿõ
m_worldSizePixels.w = lvl->GetMapSize().w * lvl->GetTileSize().w;
m_worldSizePixels.h = lvl->GetMapSize().h * lvl->GetTileSize().h;
/*
for(int layer = 0; layer < layers->size(); layer++)
//SDL_SetTextureAlphaMod(tex, layers[layer].opacity);
for(int tile = 0; tile < layers[layer].tiles.size(); tile++)
SDL_RenderCopy( renderer, tex, &layers[layer].tiles[tile].getSurfaceSrcRect(), &layers[layer].tiles[tile].getSurfaceDstRect());
*/
}
StDrawLogic::~StDrawLogic() {
glDeleteTextures(1, &m_tilesetTexture);
glDeleteTextures(1, &m_backTexture);
SDL_GL_DeleteContext(m_context);
SDL_DestroyWindow(m_window);
}