forked from gecube/opencaesar3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
oc3_gfx_gl_engine.cpp
186 lines (146 loc) · 4.71 KB
/
oc3_gfx_gl_engine.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
// This file is part of openCaesar3.
//
// openCaesar3 is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// openCaesar3 is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with openCaesar3. If not, see <http://www.gnu.org/licenses/>.
//
// Copyright 2012-2013 Gregoire Athanase, gathanase@gmail.com
#include "oc3_gfx_gl_engine.hpp"
#include <cstdlib>
#include <string>
#include <sstream>
#include <iostream>
#include <vector>
#include <SDL.h>
#include <SDL_image.h>
#include "oc3_exception.hpp"
#include "oc3_pic_loader.hpp"
#include "oc3_picture.hpp"
#include "oc3_sdl_facade.hpp"
GfxGlEngine::GfxGlEngine() : GfxEngine()
{
}
GfxGlEngine::~GfxGlEngine()
{ }
void GfxGlEngine::init()
{
int rc;
rc = SDL_Init(SDL_INIT_VIDEO);
if (rc != 0) THROW("Unable to initialize SDL: " << SDL_GetError());
rc = TTF_Init();
if (rc != 0) THROW("Unable to initialize SDL: " << SDL_GetError());
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 0);
_screen = SDL_SetVideoMode(_screen_width, _screen_height, 32, SDL_OPENGL | SDL_FULLSCREEN);
if (_screen == NULL) THROW("Unable to set video mode: " << SDL_GetError());
glEnable( GL_TEXTURE_2D );
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glViewport( 0, 0, _screen_width, _screen_height);
glClear(GL_COLOR_BUFFER_BIT); // black screen
glDisable(GL_DEPTH_TEST); // no depth test
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, _screen_width, _screen_height, 0, 0, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// Displacement trick for exact pixelization
glTranslatef(0.375, 0.375, 0);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
}
void GfxGlEngine::exit()
{
TTF_Quit();
SDL_Quit();
}
void GfxGlEngine::unload_picture(Picture &ioPicture)
{
glDeleteTextures(1, &ioPicture._glTextureID);
SDL_FreeSurface(ioPicture._surface);
ioPicture._glTextureID = 0;
ioPicture._surface = NULL;
}
void GfxGlEngine::load_picture(Picture &ioPicture)
{
GLuint &texture = ioPicture._glTextureID;
SDL_Surface *surface = ioPicture._surface;
GLenum texture_format;
GLint nOfColors;
// SDL_Surface *surface2
// get the number of channels in the SDL surface
nOfColors = surface->format->BytesPerPixel;
if (nOfColors == 4) // contains an alpha channel
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
}
else if (nOfColors == 3) // no alpha channel
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR;
}
else
{
THROW("Invalid image format");
}
if (texture == 0)
{
// the picture has no texture ID!
// generate a texture ID
glGenTextures( 1, &texture );
}
// Bind the texture object
glBindTexture( GL_TEXTURE_2D, texture );
// Set the texture's stretching properties
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
// Edit the texture object's image data using the information SDL_Surface gives us
SdlFacade::instance().lockSurface(surface);
glTexImage2D( GL_TEXTURE_2D, 0, nOfColors, surface->w, surface->h, 0,
texture_format, GL_UNSIGNED_BYTE, surface->pixels );
SdlFacade::instance().unlockSurface(surface);
}
void GfxGlEngine::init_frame()
{
glClear(GL_COLOR_BUFFER_BIT); // black screen
}
void GfxGlEngine::exit_frame()
{
SDL_GL_SwapBuffers(); //Refresh the screen
}
void GfxGlEngine::drawPicture(const Picture &picture, const int dx, const int dy)
{
GLuint aTextureID = picture._glTextureID;
int x0 = dx+picture.get_xoffset();
int x1 = x0+picture.get_width();
int y0 = dy-picture.get_yoffset();
int y1 = y0+picture.get_height();
// Bind the texture to which subsequent calls refer to
glBindTexture( GL_TEXTURE_2D, aTextureID);
glBegin( GL_QUADS );
//Bottom-left vertex (corner)
glTexCoord2i( 0, 0 );
glVertex2f( x0, y0 );
//Bottom-right vertex (corner)
glTexCoord2i( 1, 0 );
glVertex2f( x1, y0 );
//Top-right vertex (corner)
glTexCoord2i( 1, 1 );
glVertex2f( x1, y1 );
//Top-left vertex (corner)
glTexCoord2i( 0, 1 );
glVertex2f( x0, y1 );
glEnd();
}