forked from wyrover/BattleCity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRenderer.cpp
More file actions
120 lines (98 loc) · 4.89 KB
/
Renderer.cpp
File metadata and controls
120 lines (98 loc) · 4.89 KB
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
/**************************************************
Autor: Damian "RippeR" Dyńdo
URL: http://warsztat.gd/projects.php?x=view&id=2063
**************************************************/
#include <SDL/SDL.h>
#include <iostream>
#include "Game.h"
#include "Renderer.h"
using namespace std;
CRenderer::~CRenderer() {
map<string, Atlas>::iterator iter;
for(iter = m_atlasses.begin(); iter != m_atlasses.end(); ++iter) {
glDeleteTextures(1, &m_atlasses[(*iter).first].texture);
cout << "Zwolniono atlas '" << (*iter).first << "'\n";
}
}
void CRenderer::Init() {
LoadAtlasFromFile("data/sprite/sprite.bmp", "atlas");
}
void CRenderer::LoadAtlasFromFile(const string &fileName, const string &atlasName) {
//Ladowanie atlasu z pliku i dodawanie jako nowy atlas do mapy m_atlasses
Atlas NewAtlas; //Atlas który zostanie dodany
SDL_Surface* temp_surface = SDL_LoadBMP(fileName.c_str()); //Tymczasowa powierzchnia
if(!temp_surface) {
cout << "Nie mozna bylo zaladowac atlasu z pliku '" << fileName << "'!\n";
exit(1);
}
NewAtlas.width = temp_surface->w; //Szerokość atlasu
NewAtlas.height = temp_surface->h; //Wysokość atlasu
if (((NewAtlas.width & (NewAtlas.width - 1)) != 0) || //Sprawdzanie atlasu pod kątem rozmiaru
((NewAtlas.height & (NewAtlas.height - 1)) != 0)) {
cout << "Nieprawidlowy rozmiar (" << fileName << ")!\n";
exit(1);
}
GLenum format; //Format pliku bmp
switch (temp_surface->format->BytesPerPixel) {
case 3: //Bez przezroczystosci
format = GL_BGR;
break;
case 4: //Z przezroczystoscia
format = GL_BGRA;
break;
default: //Nieznany format
cout << "Nieznany format pliku (" << fileName << ")!\n";
exit(1);
}
glGenTextures(1, &NewAtlas.texture); //Tworzenie tekstury
glBindTexture(GL_TEXTURE_2D, NewAtlas.texture); //Użycie tekstury
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //Filtrowanie tekstury
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); //Przycinanie tekstury
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, temp_surface->format->BytesPerPixel, //Stworzenie obrazu tekstury
NewAtlas.width, NewAtlas.height, 0, format, GL_UNSIGNED_BYTE, temp_surface->pixels);
if (temp_surface) //Zwolnienie pamięci
SDL_FreeSurface(temp_surface);
InsertAtlas(atlasName, NewAtlas);
cout << "- Zaladowano nowy atlas '" << atlasName << "' z pliku '" << fileName << "'.\n";
}
void CRenderer::StartRendering() {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
glPushMatrix();
glBegin(GL_QUADS);
}
void CRenderer::StopRendering() {
glEnd();
glPopMatrix();
SDL_GL_SwapBuffers();
}
void CRenderer::DrawSprite(SpriteData &sprite_data, int frame, double scr_x, double scr_y, int width, int height, COLOR color) {
static int offX = CGame::Get().GameOffsetX();
static int offY = CGame::Get().GameOffsetY();
scr_x += offX;
scr_y += offY;
if(color != COLOR_NONE) {
double r,g,b;
switch(color) {
case COLOR_BLACK: r = 0; g = 0; b = 0; break;
case COLOR_RED: r = 1; g = 0; b = 0; break;
case COLOR_GREEN: r = 0; g = 1; b = 0; break;
case COLOR_BLUE: r = 0; g = 0; b = 1; break;
}
glColor3d(r,g,b);
}
//Współrzędne na teksturze, ponieważ ma ona współrzędne lewego górnego rogu (0,0) i prawego dolnego (1,1)
double left = (sprite_data.left + (sprite_data.width * frame)) / m_atlasses[sprite_data.atlas].width;
double right = left + (sprite_data.width / m_atlasses[sprite_data.atlas].width);
double bottom = sprite_data.bottom / m_atlasses[sprite_data.atlas].height;
double top = bottom - (sprite_data.height/m_atlasses[sprite_data.atlas].height);
glTexCoord2f(right, top); glVertex2f(scr_x + width, scr_y + height);
glTexCoord2f(left, top); glVertex2f(scr_x, scr_y + height);
glTexCoord2f(left, bottom); glVertex2f(scr_x, scr_y);
glTexCoord2f(right, bottom); glVertex2f(scr_x + width, scr_y);
if(color != COLOR_NONE) {
glColor3d(1,1,1);
}
}