-
Notifications
You must be signed in to change notification settings - Fork 27
/
enginegl.cpp
183 lines (151 loc) · 5.62 KB
/
enginegl.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
//////////////////////////////////////////////////////////////////////
// Yet Another Tibia Client
//////////////////////////////////////////////////////////////////////
// OpenGL engine
//////////////////////////////////////////////////////////////////////
// This program 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 2
// of the License, or (at your option) any later version.
//
// This program 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 this program; if not, write to the Free Software Foundation,
// Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
//////////////////////////////////////////////////////////////////////
/// \file enginegl.cpp
///
/// Contains the code for OpenGL rendering engine.
///
/// \sa EngineGL
#ifdef USE_OPENGL
#include <SDL/SDL_opengl.h>
#include <GLICT/globals.h>
#include <time.h>
#include "options.h"
#include "enginegl.h"
#include "font.h"
EngineGL::EngineGL()
{
DEBUGPRINT(DEBUGPRINT_LEVEL_OBLIGATORY, DEBUGPRINT_NORMAL, "Starting OpenGL engine\n");
m_screen = NULL;
SDL_ShowCursor(0);
doResize(m_width, m_height);
if(!m_screen)
// looks like GL is not supported -- dont attempt anything else
return;
glictGlobals.drawPartialOut = true;
glictGlobals.clippingMode = GLICT_SCISSORTEST;
Sprite*a,*b;
m_sysfont->SetFontParam(new YATCFont("Tibia.pic", 2, a=createSprite("Tibia.pic", 2)));
m_minifont->SetFontParam(new YATCFont("Tibia.pic", 5, createSprite("Tibia.pic", 5)));
m_aafont->SetFontParam(new YATCFont("Tibia.pic", 7, b=createSprite("Tibia.pic", 7)));
m_gamefont->SetFontParam(new YATCFont("Tibia.pic", 4, createSprite("Tibia.pic", 4)));
a->addColor(.75,.75,.75);
b->addColor(.75,.75,.75);
m_ui = createSprite("Tibia.pic", 3);
m_light = createSprite("Tibia.pic", 6);
m_cursorBasic = m_ui->createCursor(290,12,11,19, 1, 1);
m_cursorUse = m_ui->createCursor(310,12,19,19, 9, 9);
SDL_ShowCursor(1);
SDL_SetCursor(m_cursorBasic);
initEngine();
}
EngineGL::~EngineGL()
{
DEBUGPRINT(DEBUGPRINT_LEVEL_OBLIGATORY, DEBUGPRINT_NORMAL, "Closing OpenGL engine\n");
if (m_screen) {
delete (YATCFont*)m_sysfont->GetFontParam();
delete (YATCFont*)m_minifont->GetFontParam();
delete (YATCFont*)m_aafont->GetFontParam();
delete (YATCFont*)m_gamefont->GetFontParam();
}
}
void EngineGL::initEngine()
{
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
glShadeModel(GL_SMOOTH);
glClearDepth(1.0);
glDepthFunc(GL_LESS);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
}
void EngineGL::doResize(int& w, int& h)
{
Engine::doResize(w, h);
if (m_screen) SDL_FreeSurface(m_screen);
m_videoflags = SDL_OPENGL | SDL_RESIZABLE | SDL_DOUBLEBUF | SDL_HWSURFACE;
m_creationTimestamp = time(NULL);
if (options.fullscreen)
m_videoflags |= SDL_FULLSCREEN;
m_screen = SDL_SetVideoMode(m_width, m_height, m_video_bpp, m_videoflags);
if(!m_screen){
fprintf(stderr, "Could not set %dx%d video mode: %s\n", m_width, m_height, SDL_GetError());
return;
}
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0.0f, w, h, 0.0f, -1.0f, 1.0f);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void EngineGL::drawLightmap(vertex* lightmap, int type, int width, int height, float scale)
{
if (type == 3){
glDisable(GL_TEXTURE_2D);
for (int y = 0; y < height - 1; ++y){
glBegin(GL_TRIANGLE_STRIP);
for (int x = 0; x < width; ++x){
glColor4f((lightmap[(((y + 1) * width) + x)].r / lightmap[(((y + 1) * width) + x)].blended) / 255.0f, (lightmap[(((y + 1) * width) + x)].g / lightmap[(((y + 1) * width) + x)].blended) / 255.0f, (lightmap[(((y + 1) * width) + x)].b / lightmap[(((y + 1) * width) + x)].blended) / 255.0f, lightmap[(((y + 1) * width) + x)].alpha / 255.0f);
glVertex2f(lightmap[(((y + 1) * width) + x)].x, lightmap[(((y + 1) * width) + x)].y);
glColor4f((lightmap[((y * width) + x)].r / lightmap[((y * width) + x)].blended) / 255.0f, (lightmap[((y * width) + x)].g / lightmap[((y * width) + x)].blended) / 255.0f, (lightmap[((y * width) + x)].b / lightmap[((y * width) + x)].blended) / 255.0f, lightmap[((y * width) + x)].alpha / 255.0f);
glVertex2f(lightmap[((y * width) + x)].x, lightmap[((y * width) + x)].y);
}
glEnd();
}
}
else{
Engine::drawLightmap(lightmap, type, width, height, scale);
}
}
void EngineGL::drawRectangle(float x, float y, float width, float height, oRGBA color)
{
glDisable(GL_TEXTURE_2D);
glColor4f(color.r/255.0f, color.g/255.0f, color.b/255.0f, color.a/255.0f);
glRectf(x, y, x+width, y+height);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
void EngineGL::drawRectangleLines(float x, float y, float width, float height, oRGBA color, float thickness /*= 1.f*/)
{
glDisable(GL_TEXTURE_2D);
glColor4f(color.r/255.0f, color.g/255.0f, color.b/255.0f, color.a/255.0f);
//TODO (nfries88): implement thickness
glBegin(GL_LINE_LOOP);
glVertex2f(x,y);
glVertex2f(x+width,y);
glVertex2f(x+width,y+height);
glVertex2f(x,y+height);
glEnd();
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
}
bool EngineGL::isSupported()
{
if (!m_screen)
return false;
else
return true;
/*uint32_t vf = SDL_OPENGL | SDL_RESIZABLE;
SDL_Surface *s = SDL_SetVideoMode(m_width, m_height, m_video_bpp, m_videoflags);
if (s) {
SDL_FreeSurface(s);
return true;
} else
return false;*/
}
#endif