Browse Source

basic drawing functions

powerfull Point, Rect, Size, Color classes
prerequesites header
Eduardo Bart 10 years ago
parent
commit
3d0b191199

+ 62
- 0
src/color.h View File

@@ -0,0 +1,62 @@
1
+/* The MIT License
2
+ *
3
+ * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ * THE SOFTWARE.
22
+ */
23
+
24
+
25
+#ifndef COLOR_H
26
+#define COLOR_H
27
+
28
+#include "prerequisites.h"
29
+
30
+typedef uint32 RGBA;
31
+
32
+class Color
33
+{
34
+public:
35
+    Color() : color(0) { }
36
+    Color(uint8 r, uint8 g, uint8 b, uint8 a = 0xFF) : color(((r & 0xff)<<24) | ((g & 0xff)<<16) | ((b & 0xff)<<8) | (a & 0xff)) { }
37
+    Color(const Color& other) : color(other.color) { }
38
+    Color(RGBA rgba) : color(rgba) { }
39
+
40
+    uint8 red()   const { return (color >> 24) & 0xFF; }
41
+    uint8 green() const { return (color >> 16) & 0xFF; }
42
+    uint8 blue()  const { return (color >> 8) & 0xFF; }
43
+    uint8 alpha() const { return color & 0xFF; }
44
+    RGBA rgba() const { return color; }
45
+    const uint8* rgbaPtr() const { return (const uint8*)&color; }
46
+
47
+    void setRed(uint8 r) { color = ((r & 0xff)<<24) | (color & 0x00ffffff); }
48
+    void setGreen(uint8 g)   { color = ((g & 0xff)<<16) | (color & 0xff00ffff); }
49
+    void setBlue(uint8 b) { color = ((b & 0xff)<<8) | (color & 0xffff00ff); }
50
+    void setAlpha(uint8 a)  { color = (a & 0xff) | (color & 0xffffff00); }
51
+    void setRGBA(uint8 r, uint8 g, uint8 b, uint8 a = 0xFF) { color = ((r & 0xff)<<24) | ((g & 0xff)<<16) | ((b & 0xff)<<8) | (a & 0xff); }
52
+    void setRGBA(uint32 rgba) { color = rgba; }
53
+
54
+    Color& operator=(const Color& other) { color = other.color;  return *this; }
55
+    bool operator==(const Color& other) const { return other.color == color; }
56
+    bool operator!=(const Color& other) const { return other.color != color; }
57
+
58
+private:
59
+    RGBA color;
60
+};
61
+
62
+#endif // COLOR_H

+ 6
- 7
src/configmanager.cpp View File

@@ -23,12 +23,9 @@
23 23
 
24 24
 
25 25
 #include "configmanager.h"
26
-#include "logger.h"
27
-#include "util.h"
26
+#include "resourcemanager.h"
28 27
 
29
-#include <fstream>
30 28
 #include <yaml-cpp/yaml.h>
31
-#include "resourcemanager.h"
32 29
 
33 30
 ConfigManager g_config;
34 31
 
@@ -77,9 +74,11 @@ bool ConfigManager::load(const std::string& fileName)
77 74
 
78 75
 void ConfigManager::save()
79 76
 {
80
-    YAML::Emitter out;
81
-    out << m_confsMap;
82
-    g_resources.saveFile(m_fileName, (const unsigned char*)out.c_str(), out.size());
77
+    if(!m_fileName.empty()) {
78
+        YAML::Emitter out;
79
+        out << m_confsMap;
80
+        g_resources.saveFile(m_fileName, (const unsigned char*)out.c_str(), out.size());
81
+    }
83 82
 }
84 83
 
85 84
 void ConfigManager::setValue(const std::string &key, const std::string &value)

+ 1
- 2
src/configmanager.h View File

@@ -25,8 +25,7 @@
25 25
 #ifndef CONFIGMANAGER_H
26 26
 #define CONFIGMANAGER_H
27 27
 
28
-#include <string>
29
-#include <map>
28
+#include "prerequisites.h"
30 29
 
31 30
 class ConfigManager
32 31
 {

+ 7
- 8
src/engine.cpp View File

@@ -25,10 +25,8 @@
25 25
 #include "engine.h"
26 26
 #include "platform.h"
27 27
 #include "graphics.h"
28
-#include "const.h"
29 28
 #include "input.h"
30 29
 #include "configmanager.h"
31
-#include "logger.h"
32 30
 #include "gamestate.h"
33 31
 
34 32
 Engine g_engine;
@@ -36,7 +34,6 @@ Engine g_engine;
36 34
 Engine::Engine() :
37 35
     m_stopping(false),
38 36
     m_running(false),
39
-    m_lastFrameTicks(0),
40 37
     m_currentState(NULL)
41 38
 {
42 39
 }
@@ -82,18 +79,19 @@ void Engine::terminate()
82 79
 void Engine::run()
83 80
 {
84 81
     unsigned long ticks;
82
+    static unsigned long lastFrameTicks;
85 83
 
86 84
     m_running = true;
87
-    m_lastFrameTicks = Platform::getTicks();
88 85
 
86
+    lastFrameTicks = Platform::getTicks();
89 87
     while(!m_stopping) {
90 88
         // fire platform events
91 89
         Platform::poll();
92 90
 
93 91
         // update
94 92
         ticks = Platform::getTicks();
95
-        update(ticks - m_lastFrameTicks);
96
-        m_lastFrameTicks = ticks;
93
+        update(ticks - lastFrameTicks);
94
+        lastFrameTicks = ticks;
97 95
 
98 96
         // render
99 97
         render();
@@ -102,7 +100,7 @@ void Engine::run()
102 100
         Platform::swapBuffers();
103 101
     }
104 102
 
105
-    m_lastFrameTicks = 0;
103
+    lastFrameTicks = 0;
106 104
     m_stopping = false;
107 105
     m_running = false;
108 106
 }
@@ -117,7 +115,8 @@ void Engine::changeState(GameState* newState)
117 115
     if(m_currentState)
118 116
         m_currentState->onLeave();
119 117
     m_currentState = newState;
120
-    m_currentState->onEnter();
118
+    if(m_currentState)
119
+        m_currentState->onEnter();
121 120
 }
122 121
 
123 122
 void Engine::render()

+ 2
- 2
src/engine.h View File

@@ -25,6 +25,8 @@
25 25
 #ifndef ENGINE_H
26 26
 #define ENGINE_H
27 27
 
28
+#include "prerequisites.h"
29
+
28 30
 struct InputEvent;
29 31
 
30 32
 class GameState;
@@ -66,8 +68,6 @@ private:
66 68
     bool m_stopping;
67 69
     bool m_running;
68 70
 
69
-    unsigned long m_lastFrameTicks;
70
-
71 71
     GameState *m_currentState;
72 72
 };
73 73
 

+ 0
- 6
src/framebuffer.cpp View File

@@ -21,15 +21,9 @@
21 21
  * THE SOFTWARE.
22 22
  */
23 23
 
24
-#define GL_GLEXT_PROTOTYPES
25
-
26 24
 #include "framebuffer.h"
27 25
 #include "platform.h"
28 26
 #include "graphics.h"
29
-#include "logger.h"
30
-
31
-#include <GL/glext.h>
32
-#include <GL/glu.h>
33 27
 
34 28
 FrameBuffer::FrameBuffer(int width, int height)
35 29
 {

+ 1
- 1
src/framebuffer.h View File

@@ -25,7 +25,7 @@
25 25
 #ifndef FRAMEBUFFER_H
26 26
 #define FRAMEBUFFER_H
27 27
 
28
-#include <GL/gl.h>
28
+#include "prerequisites.h"
29 29
 
30 30
 class FrameBuffer
31 31
 {

+ 111
- 7
src/graphics.cpp View File

@@ -24,9 +24,6 @@
24 24
 
25 25
 #include "graphics.h"
26 26
 #include "logger.h"
27
-
28
-#include <GL/gl.h>
29
-#include <GL/glu.h>
30 27
 #include "texture.h"
31 28
 
32 29
 Graphics g_graphics;
@@ -60,15 +57,18 @@ void Graphics::terminate()
60 57
 
61 58
 void Graphics::resize(int width, int height)
62 59
 {
63
-    m_width = width;
64
-    m_height = height;
60
+    m_screenSize.setWidth(width);
61
+    m_screenSize.setHeight(height);
65 62
     restoreViewport();
66 63
 }
67 64
 
68 65
 void Graphics::restoreViewport()
69 66
 {
67
+    const int& width = m_screenSize.width();
68
+    const int& height = m_screenSize.height();
69
+
70 70
     // resize gl viewport
71
-    glViewport(0, 0, m_width, m_height);
71
+    glViewport(0, 0, width, height);
72 72
 
73 73
     /*
74 74
      0,0---------0,w
@@ -80,7 +80,7 @@ void Graphics::restoreViewport()
80 80
     // setup view region like above
81 81
     glMatrixMode(GL_PROJECTION);
82 82
     glLoadIdentity();
83
-    gluOrtho2D(0.0f, m_width, m_height, 0.0f);
83
+    gluOrtho2D(0.0f, width, height, 0.0f);
84 84
 
85 85
     // back to model view
86 86
     glMatrixMode(GL_MODELVIEW);
@@ -98,3 +98,107 @@ void Graphics::endRender()
98 98
 {
99 99
 
100 100
 }
101
+
102
+void Graphics::drawTexturedRect(const Rect& screenCoords, const Texture *texture, const Rect& texCoords)
103
+{
104
+    // rect correction for opengl
105
+    int right = screenCoords.right()+1;
106
+    int bottom = screenCoords.bottom()+1;
107
+    int top = screenCoords.top();
108
+    int left = screenCoords.left();
109
+
110
+    float tright;
111
+    float tbottom;
112
+    float ttop;
113
+    float tleft;
114
+
115
+    if(!texCoords.isEmpty()) {
116
+        const Size& textureSize = texture->getSize();
117
+        tright = (float)(texCoords.right()+1)/textureSize.width();
118
+        tbottom = (float)(texCoords.bottom()+1)/textureSize.height();
119
+        ttop = (float)texCoords.top()/textureSize.height();
120
+        tleft = (float)texCoords.left()/textureSize.width();
121
+    } else {
122
+        tright = 0.0f;
123
+        tbottom = 1.0f;
124
+        ttop = 0.0f;
125
+        tleft = 1.0f;
126
+    }
127
+
128
+    glBindTexture(GL_TEXTURE_2D, texture->getTextureId());
129
+    glBegin(GL_QUADS);
130
+    glTexCoord2f(tleft,  ttop); glVertex2i(left,  top);
131
+    glTexCoord2f(tleft,  tbottom); glVertex2i(left,  bottom);
132
+    glTexCoord2f(tright, tbottom); glVertex2i(right, bottom);
133
+    glTexCoord2f(tright, ttop); glVertex2i(right, top);
134
+    glEnd();
135
+}
136
+
137
+void Graphics::drawColoredRect(const Rect& screenCoords, const Color& color)
138
+{
139
+    glDisable(GL_TEXTURE_2D);
140
+
141
+    glColor4ubv(color.rgbaPtr());
142
+
143
+    // rect correction for opengl
144
+    int right = screenCoords.right()+1;
145
+    int bottom = screenCoords.bottom()+1;
146
+    int top = screenCoords.top();
147
+    int left = screenCoords.left();
148
+
149
+    glBegin(GL_QUADS);
150
+    glVertex2i(left,  top);
151
+    glVertex2i(left,  bottom);
152
+    glVertex2i(right, bottom);
153
+    glVertex2i(right, top);
154
+    glEnd();
155
+
156
+    glEnable(GL_TEXTURE_2D);
157
+}
158
+
159
+
160
+void Graphics::drawBoundingRect(const Rect& screenCoords, const Color& color, int lineWidth)
161
+{
162
+    if(2*lineWidth > screenCoords.height())
163
+        return;
164
+
165
+    glDisable(GL_TEXTURE_2D);
166
+
167
+    glColor4ubv(color.rgbaPtr());
168
+
169
+    // rect correction for opengl
170
+    int right = screenCoords.right()+1;
171
+    int bottom = screenCoords.bottom()+1;
172
+    int top = screenCoords.top();
173
+    int left = screenCoords.left();
174
+
175
+    glBegin(GL_QUADS);
176
+
177
+    // top line
178
+    glVertex2i(left,  top);
179
+    glVertex2i(left,  top+lineWidth);
180
+    glVertex2i(right, top+lineWidth);
181
+    glVertex2i(right, top);
182
+
183
+    // left
184
+    glVertex2i(left, screenCoords.top()+lineWidth);
185
+    glVertex2i(left, bottom-lineWidth);
186
+    glVertex2i(left+lineWidth, bottom-lineWidth);
187
+    glVertex2i(left+lineWidth, screenCoords.top()+lineWidth);
188
+
189
+    // bottom line
190
+    glVertex2i(left,  bottom);
191
+    glVertex2i(left,  bottom-lineWidth);
192
+    glVertex2i(right, bottom-lineWidth);
193
+    glVertex2i(right, bottom);
194
+
195
+    // right line
196
+    glVertex2i(right,           top+lineWidth);
197
+    glVertex2i(right,           bottom-lineWidth);
198
+    glVertex2i(right-lineWidth, bottom-lineWidth);
199
+    glVertex2i(right-lineWidth, top+lineWidth);
200
+
201
+    glEnd();
202
+
203
+    glEnable(GL_TEXTURE_2D);
204
+}

+ 10
- 4
src/graphics.h View File

@@ -25,6 +25,11 @@
25 25
 #ifndef GRAPHICS_H
26 26
 #define GRAPHICS_H
27 27
 
28
+#include "prerequisites.h"
29
+#include "rect.h"
30
+#include "size.h"
31
+#include "color.h"
32
+
28 33
 class Texture;
29 34
 
30 35
 class Graphics
@@ -48,12 +53,13 @@ public:
48 53
     /// Called after every render
49 54
     void endRender();
50 55
 
51
-    int getWidth() { return m_width; }
52
-    int getHeight() { return m_height; }
56
+    const Size& getScreenSize() const { return m_screenSize; }
57
+    void drawTexturedRect(const Rect& screenCoords, const Texture *texture, const Rect& texCoords = Rect());
58
+    void drawColoredRect(const Rect& screenCoords, const Color& color);
59
+    void drawBoundingRect(const Rect& screenCoords, const Color& color, int lineWidth);
53 60
 
54 61
 private:
55
-    int m_width;
56
-    int m_height;
62
+    Size m_screenSize;
57 63
 };
58 64
 
59 65
 extern Graphics g_graphics;

+ 0
- 4
src/logger.cpp View File

@@ -23,10 +23,6 @@
23 23
 
24 24
 
25 25
 #include "logger.h"
26
-#include "util.h"
27
-
28
-#include <cstdarg>
29
-#include <cstdlib>
30 26
 #include <boost/algorithm/string.hpp>
31 27
 
32 28
 void Logger::log(int level, const char *trace, const char *format, ...)

+ 1
- 3
src/logger.h View File

@@ -25,9 +25,7 @@
25 25
 #ifndef LOGGER_H
26 26
 #define LOGGER_H
27 27
 
28
-#include <iostream>
29
-#include <sstream>
30
-#include <string>
28
+#include "prerequisites.h"
31 29
 
32 30
 namespace Logger {
33 31
 

+ 0
- 2
src/main.cpp View File

@@ -23,8 +23,6 @@
23 23
 
24 24
 
25 25
 #include "engine.h"
26
-#include "const.h"
27
-#include "logger.h"
28 26
 #include "configmanager.h"
29 27
 #include "resourcemanager.h"
30 28
 #include "platform.h"

+ 16
- 40
src/menustate.cpp View File

@@ -28,8 +28,8 @@
28 28
 #include "texturemanager.h"
29 29
 #include "logger.h"
30 30
 #include "engine.h"
31
+#include "rect.h"
31 32
 
32
-FrameBuffer *fbo;
33 33
 TexturePtr background;
34 34
 
35 35
 MenuState::MenuState()
@@ -44,8 +44,8 @@ MenuState::~MenuState()
44 44
 
45 45
 void MenuState::onEnter()
46 46
 {
47
-    background = g_textures.get("background.png");
48
-    background->enableBilinearFilter();
47
+    m_background = g_textures.get("background.png");
48
+    m_background->enableBilinearFilter();
49 49
 }
50 50
 
51 51
 void MenuState::onLeave()
@@ -64,43 +64,19 @@ void MenuState::onInputEvent(InputEvent* event)
64 64
 
65 65
 void MenuState::render()
66 66
 {
67
-    // draw background
68
-    background->bind();
69
-
70
-    int x = 0;
71
-    int y = 0;
72
-    int screenWidth = g_graphics.getWidth();
73
-    int screenHeight = g_graphics.getHeight();
74
-    int textureWidth = background->getWidth();
75
-    int textureHeight = background->getHeight();
76
-
77
-    int texCoordX;
78
-    int texCoordY;
79
-    int texCoordWidth;
80
-    int texCoordHeight;
81
-
82
-    int wantedWidth = 1240;
83
-    int wantedHeight = 880;
84
-
85
-    float originalRatio = (float)wantedWidth/wantedHeight;
86
-    float screenRatio = (float)screenWidth/screenHeight;
87
-    if(screenRatio >= originalRatio) {
88
-        texCoordHeight = wantedHeight;
89
-        texCoordWidth = std::min((int)(wantedHeight*screenRatio), textureWidth);
90
-    } else {
91
-        texCoordWidth = wantedWidth;
92
-        texCoordHeight = std::min((int)(wantedWidth/screenRatio), textureHeight);
93
-    }
94
-    texCoordX = textureWidth - texCoordWidth;
95
-    texCoordY = textureHeight - texCoordHeight;
96
-
97
-
98
-    glBegin(GL_QUADS);
99
-    glTexCoord2f((float)texCoordX/textureWidth,                 (float)texCoordY/textureHeight); glVertex2i(x,       y);
100
-    glTexCoord2f((float)texCoordX/textureWidth,                 (float)(texCoordY+texCoordHeight)/textureHeight); glVertex2i(x,       y+screenHeight);
101
-    glTexCoord2f((float)(texCoordX+texCoordWidth)/textureWidth, (float)(texCoordY+texCoordHeight)/textureHeight); glVertex2i(x+screenWidth, y+screenHeight);
102
-    glTexCoord2f((float)(texCoordX+texCoordWidth)/textureWidth, (float)texCoordY/textureHeight); glVertex2i(x+screenWidth, y);
103
-    glEnd();
67
+    static Size minTexCoordsSize(1240, 880);
68
+    const Size& screenSize = g_graphics.getScreenSize();
69
+    Size texSize = m_background->getSize();
70
+
71
+    Size texCoordsSize = screenSize;
72
+    if(texCoordsSize < minTexCoordsSize)
73
+        texCoordsSize.scale(minTexCoordsSize, KEEP_ASPECT_RATIO_BY_EXPANDING);
74
+    texCoordsSize = texCoordsSize.boundedTo(texSize);
75
+
76
+    Rect texCoords(0, 0, texCoordsSize);
77
+    texCoords.moveBottomRight(texSize.toPoint());
78
+
79
+    g_graphics.drawTexturedRect(Rect(0, 0, screenSize), m_background.get(), texCoords);
104 80
 }
105 81
 
106 82
 void MenuState::update(int elapsedTicks)

+ 5
- 0
src/menustate.h View File

@@ -25,7 +25,9 @@
25 25
 #ifndef MENUSTATE_H
26 26
 #define MENUSTATE_H
27 27
 
28
+#include "prerequisites.h"
28 29
 #include "gamestate.h"
30
+#include "texture.h"
29 31
 
30 32
 class MenuState : public GameState
31 33
 {
@@ -42,6 +44,9 @@ public:
42 44
 
43 45
     void render();
44 46
     void update(int elapsedTicks);
47
+
48
+private:
49
+    TexturePtr m_background;
45 50
 };
46 51
 
47 52
 #endif // MENUSTATE_H

+ 2
- 1
src/platform.h View File

@@ -25,7 +25,8 @@
25 25
 #ifndef PLATFORM_H
26 26
 #define PLATFORM_H
27 27
 
28
-// namespace with platform specific stuff
28
+#include "prerequisites.h"
29
+
29 30
 namespace Platform
30 31
 {
31 32
     void init();

+ 79
- 0
src/point.h View File

@@ -0,0 +1,79 @@
1
+/* The MIT License
2
+ *
3
+ * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ * THE SOFTWARE.
22
+ */
23
+
24
+
25
+#ifndef POINT_H
26
+#define POINT_H
27
+
28
+#include "prerequisites.h"
29
+
30
+template <class T>
31
+class TSize;
32
+
33
+template <class T>
34
+class TPoint
35
+{
36
+public:
37
+    inline TPoint() : x(0), y(0) {};
38
+    inline TPoint(T x, T y) : x(x), y(y) { };
39
+    inline TPoint(const TPoint<T>& other) : x(other.x), y(other.y) { };
40
+
41
+    inline bool isNull() const { return x==0 && y==0; }
42
+    inline TSize<T> toSize() const { return TSize<T>(x, y); }
43
+
44
+    inline TPoint<T> operator-() const { return TPoint<T>(-x, -y); }
45
+    inline TPoint<T> operator+(const TPoint<T>& other) const { return TPoint<T>(x + other.x, y + other.y);   }
46
+    inline TPoint<T>& operator+=(const TPoint<T>& other) { x+=other.x; y+=other.y; return *this; }
47
+    inline TPoint<T> operator-(const TPoint<T>& other) const { return TPoint<T>(x - other.x, y - other.y);   }
48
+    inline TPoint<T>& operator-=(const TPoint<T>& other) { x-=other.x; y-=other.y; return *this; }
49
+    inline TPoint<T> operator*(const TPoint<T>& other) const { return TPoint<T>(x * other.x, y * other.y);   }
50
+    inline TPoint<T>& operator*=(const TPoint<T>& other) { x*=other.x; y*=other.y; return *this; }
51
+    inline TPoint<T> operator*(const T v) const { return TPoint<T>(x * v, y * v);  }
52
+    inline TPoint<T>& operator*=(const T v) { x*=v; y*=v; return *this; }
53
+    inline TPoint<T> operator/(const TPoint<T>& other) const { return TPoint<T>(x/other.x, y/other.y);   }
54
+    inline TPoint<T>& operator/=(const TPoint<T>& other)   { x/=other.x; y/=other.y; return *this; }
55
+    inline TPoint<T> operator/(const T v) const { return TPoint<T>(x/v, y/v);  }
56
+    inline TPoint<T>& operator/=(const T v) { x/=v; y/=v; return *this; }
57
+
58
+    inline bool operator<=(const TPoint<T>&other) const { return x<=other.x && y<=other.y; }
59
+    inline bool operator>=(const TPoint<T>&other) const { return x>=other.x && y>=other.y; }
60
+    inline bool operator<(const TPoint<T>&other) const { return x<other.x && y<other.y; }
61
+    inline bool operator>(const TPoint<T>&other) const { return x>other.x && y>other.y; }
62
+
63
+    inline TPoint<T>& operator=(const TPoint<T>& other) { x = other.x; y = other.y; return *this; }
64
+    inline bool operator==(const TPoint<T>& other) const { return other.x==x && other.y==y; }
65
+    inline bool operator!=(const TPoint<T>& other) const { return other.x!=x || other.y!=y; }
66
+
67
+    inline float length() const { return sqrtf((float)(x*x + y*y)); }
68
+
69
+    inline float distanceFrom(const TPoint<T>& other) const {
70
+        return TPoint<T>(x - other.x, y - other.y).getLength();
71
+    }
72
+
73
+    T x, y;
74
+};
75
+
76
+typedef TPoint<int> Point;
77
+typedef TPoint<float> PointF;
78
+
79
+#endif

src/const.h → src/prerequisites.h View File

@@ -22,13 +22,60 @@
22 22
  */
23 23
 
24 24
 
25
-#ifndef VERSION_H
26
-#define VERSION_H
25
+#ifndef PREREQUISITES_H
26
+#define PREREQUISITES_H
27 27
 
28
-#define APP_VERSION "0.1.0"
28
+// app name
29 29
 #define APP_NAME "OTClient"
30 30
 #define APP_LONGNAME APP_NAME " " APP_VERSION
31
-#define APP_ICON "data/otclient.bmp"
32 31
 
33
-#endif
32
+// app version
33
+#define APP_VERSION "0.1.0"
34
+
35
+// int types
36
+#include <stdint.h>
37
+
38
+typedef unsigned char uchar;
39
+typedef unsigned short ushort;
40
+typedef unsigned int uint;
41
+typedef unsigned long ulong;
42
+
43
+typedef uint32_t uint32;
44
+typedef uint16_t uint16;
45
+typedef uint8_t uint8;
46
+typedef int32_t int32;
47
+typedef int16_t int16;
48
+typedef int8_t int8;
49
+
50
+// c headers
51
+#include <cassert>
52
+#include <cstdio>
53
+#include <cstdlib>
54
+#include <ctime>
55
+#include <cstring>
56
+#include <cstdarg>
57
+#include <cmath>
58
+#include <algorithm>
59
+#include <fstream>
60
+#include <iostream>
61
+#include <sstream>
62
+
63
+// stl headers
64
+#include <string>
65
+#include <vector>
66
+#include <map>
67
+#include <string>
68
+#include <list>
69
+
70
+// GL stuff
71
+#define GL_GLEXT_PROTOTYPES
72
+
73
+#include <GL/gl.h>
74
+#include <GL/glu.h>
75
+#include <GL/glext.h>
76
+
77
+// utilities
78
+#include "logger.h"
79
+#include "util.h"
34 80
 
81
+#endif // PREREQUISITES_H

+ 284
- 0
src/rect.h View File

@@ -0,0 +1,284 @@
1
+/* The MIT License
2
+ *
3
+ * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ * THE SOFTWARE.
22
+ */
23
+
24
+
25
+#ifndef RECT_H
26
+#define RECT_H
27
+
28
+#include "prerequisites.h"
29
+#include "point.h"
30
+#include "size.h"
31
+
32
+template <class T>
33
+class TPoint;
34
+
35
+template <class T>
36
+class TSize;
37
+
38
+template <class T>
39
+class TRect
40
+{
41
+public:
42
+    inline TRect() : x1(0), y1(0), x2(-1), y2(-1) { }
43
+    inline TRect(T x, T y, T width, T height) : x1(x), y1(y), x2(x+width-1), y2(y+height-1) { }
44
+    inline TRect(const TPoint<T>& topLeft, const TPoint<T>& bottomRight) : x1(topLeft.x), y1(topLeft.y), x2(bottomRight.x), y2(bottomRight.y) { }
45
+    inline TRect(const TRect<T>& other) : x1(other.x1), y1(other.y1), x2(other.x2), y2(other.y2) { }
46
+    inline TRect(T x, T y, const TSize<T>& size) : x1(x), y1(y), x2(x+size.width()-1), y2(y+size.height()-1) { }
47
+    inline TRect(const TPoint<T>& topLeft, const TSize<T>& size) : x1(topLeft.x), y1(topLeft.y), x2(x+size.width()-1), y2(y+size.height()-1) { }
48
+
49
+    inline bool isNull() const { return x2 == x1 - 1 && y2 == y1 - 1; }
50
+    inline bool isEmpty() const { return x1 > x2 || y1 > y2; }
51
+    inline bool isValid() const { return x1 <= x2 && y1 <= y2; }
52
+
53
+    inline T left() const { return x1; }
54
+    inline T top() const { return y1; }
55
+    inline T right() const { return x2; }
56
+    inline T bottom() const { return y2; }
57
+    inline T x() const { return x1; }
58
+    inline T y() const { return y1; }
59
+    inline TPoint<T> topLeft() const { return TPoint<T>(x1, y1); }
60
+    inline TPoint<T> bottomRight() const { return TPoint<T>(x2, y2); }
61
+    inline TPoint<T> topRight() const { return TPoint<T>(x2, y1); }
62
+    inline TPoint<T> bottomLeft() const { return TPoint<T>(x1, y2); }
63
+    inline TPoint<T> center() const { return TPoint<T>((x1+x2)/2, (y1+y2)/2); }
64
+    inline T width() const { return  x2 - x1 + 1; }
65
+    inline T height() const { return  y2 - y1 + 1; }
66
+    inline TSize<T> size() const { return TSize<T>(width(), height()); }
67
+
68
+    inline void setLeft(T pos) { x1 = pos; }
69
+    inline void setTop(T pos) { y1 = pos; }
70
+    inline void setRight(T pos) { x2 = pos; }
71
+    inline void setBottom(T pos) { y2 = pos; }
72
+    inline void setX(T x) { x1 = x; }
73
+    inline void setY(T y) { y1 = y; }
74
+    inline void setTopLeft(const TPoint<T> &p) { x1 = p.x; y1 = p.y; }
75
+    inline void setBottomRight(const TPoint<T> &p) { x2 = p.x; y2 = p.y; }
76
+    inline void setTopRight(const TPoint<T> &p) { x2 = p.x; y1 = p.y; }
77
+    inline void setBottomLeft(const TPoint<T> &p) { x1 = p.x; y2 = p.y; }
78
+    inline void setWidth(T width) { x2 = x1 + width - 1; }
79
+    inline void setHeight(T height) { y2 = y1 + height- 1; }
80
+    inline void setSize(T width, T height) { x2 = x1 + width; y2 = y1 + height; }
81
+    inline void setSize(const TSize<T>& size) { x2 = x1 + size.width(); y2 = y1 + size.height(); }
82
+    inline void setRect(T x, T y, T width, T height) { x1 = x; y1 = y; x2 = (x + width - 1); y2 = (y + height - 1); }
83
+    inline void setCoords(int left, int top, int right, int bottom) { x1 = left; y1 = top; x2 = right; y2 = bottom; }
84
+
85
+    inline void translate(T x, T y) { x1 += x; y1 += y; x2 += x; y2 += y; }
86
+    inline void translate(const TPoint<T> &p) { x1 += p.x; y1 += p.y; x2 += p.x; y2 += p.y; }
87
+    inline void moveTo(T x, T y) { x2 += x - x1; y2 += y - y1; x1 = x; y1 = y; }
88
+    inline void moveTo(const TPoint<T> &p) { x2 += p.x - x1; y2 += p.y - y1; x1 = p.x; y1 = p.y; }
89
+    inline void moveLeft(T pos) { x2 += (pos - x1); x1 = pos; }
90
+    inline void moveTop(T pos) { y2 += (pos - y1); y1 = pos; }
91
+    inline void moveRight(T pos) { x1 += (pos - x2); x2 = pos; }
92
+    inline void moveBottom(T pos) { y1 += (pos - y2); y2 = pos; }
93
+    inline void moveTopLeft(const TPoint<T> &p) { moveLeft(p.x); moveTop(p.y); }
94
+    inline void moveBottomRight(const TPoint<T> &p) { moveRight(p.x); moveBottom(p.y); }
95
+    inline void moveTopRight(const TPoint<T> &p) { moveRight(p.x); moveTop(p.y); }
96
+    inline void moveBottomLeft(const TPoint<T> &p) { moveLeft(p.x); moveBottom(p.y); }
97
+
98
+    inline TRect<T> translated(int x, int y) const { return TRect<T>(TPoint<T>(x1 + x, y1 + y), TPoint<T>(x2 + x, y2 + y)); }
99
+    inline TRect<T> translated(const TPoint<T> &p) const { return TRect<T>(TPoint<T>(x1 + p.x(), y1 + p.y()), TPoint<T>(x2 + p.x(), y2 + p.y())); }
100
+
101
+    void moveCenter(const TPoint<T> &p) {
102
+        T w = x2 - x1;
103
+        T h = y2 - y1;
104
+        x1 = p.x() - w/2;
105
+        y1 = p.y() - h/2;
106
+        x2 = x1 + w;
107
+        y2 = y1 + h;
108
+    }
109
+
110
+    bool contains(const TPoint<T> &p, bool insideOnly = false) const {
111
+        T l, r;
112
+        if(x2 < x1 - 1) {
113
+            l = x2;
114
+            r = x1;
115
+        } else {
116
+            l = x1;
117
+            r = x2;
118
+        }
119
+        if(insideOnly) {
120
+            if(p.x() <= l || p.x() >= r)
121
+                return false;
122
+        } else {
123
+            if(p.x() < l || p.x() > r)
124
+                return false;
125
+        }
126
+        int t, b;
127
+        if(y2 < y1 - 1) {
128
+            t = y2;
129
+            b = y1;
130
+        } else {
131
+            t = y1;
132
+            b = y2;
133
+        }
134
+        if(insideOnly) {
135
+            if(p.y() <= t || p.y() >= b)
136
+                return false;
137
+        } else {
138
+            if(p.y() < t || p.y() > b)
139
+                return false;
140
+        }
141
+        return true;
142
+    }
143
+
144
+    bool intersects(const TRect<T> &r) const {
145
+        if(isNull() || r.isNull())
146
+            return false;
147
+
148
+        int l1 = x1;
149
+        int r1 = x1;
150
+        if(x2 - x1 + 1 < 0)
151
+            l1 = x2;
152
+        else
153
+            r1 = x2;
154
+
155
+        int l2 = r.x1;
156
+        int r2 = r.x1;
157
+        if(r.x2 - r.x1 + 1 < 0)
158
+            l2 = r.x2;
159
+        else
160
+            r2 = r.x2;
161
+
162
+        if (l1 > r2 || l2 > r1)
163
+            return false;
164
+
165
+        int t1 = y1;
166
+        int b1 = y1;
167
+        if(y2 - y1 + 1 < 0)
168
+            t1 = y2;
169
+        else
170
+            b1 = y2;
171
+
172
+        int t2 = r.y1;
173
+        int b2 = r.y1;
174
+        if (r.y2 - r.y1 + 1 < 0)
175
+            t2 = r.y2;
176
+        else
177
+            b2 = r.y2;
178
+
179
+        if(t1 > b2 || t2 > b1)
180
+            return false;
181
+
182
+        return true;
183
+    }
184
+
185
+    TRect<T> united(const TRect<T> &r) const {
186
+        if(isNull() || r.isNull())
187
+            return TRect<T>();
188
+
189
+        int l1 = x1;
190
+        int r1 = x1;
191
+        if (x2 - x1 + 1 < 0)
192
+            l1 = x2;
193
+        else
194
+            r1 = x2;
195
+
196
+        int l2 = r.x1;
197
+        int r2 = r.x1;
198
+        if(r.x2 - r.x1 + 1 < 0)
199
+            l2 = r.x2;
200
+        else
201
+            r2 = r.x2;
202
+
203
+        if(l1 > r2 || l2 > r1)
204
+            return TRect<T>();
205
+
206
+        int t1 = y1;
207
+        int b1 = y1;
208
+        if(y2 - y1 + 1 < 0)
209
+            t1 = y2;
210
+        else
211
+            b1 = y2;
212
+
213
+        int t2 = r.y1;
214
+        int b2 = r.y1;
215
+        if(r.y2 - r.y1 + 1 < 0)
216
+            t2 = r.y2;
217
+        else
218
+            b2 = r.y2;
219
+
220
+        if(t1 > b2 || t2 > b1)
221
+            return TRect<T>();
222
+
223
+        TRect<T> tmp;
224
+        tmp.x1 = std::max(l1, l2);
225
+        tmp.x2 = std::min(r1, r2);
226
+        tmp.y1 = std::max(t1, t2);
227
+        tmp.y2 = std::min(b1, b2);
228
+        return tmp;
229
+    }
230
+
231
+    TRect<T> intersection(const TRect<T> &r) const {
232
+        if(isNull())
233
+            return r;
234
+        if(r.isNull())
235
+            return *this;
236
+
237
+        int l1 = x1;
238
+        int r1 = x1;
239
+        if(x2 - x1 + 1 < 0)
240
+            l1 = x2;
241
+        else
242
+            r1 = x2;
243
+
244
+        int l2 = r.x1;
245
+        int r2 = r.x1;
246
+        if(r.x2 - r.x1 + 1 < 0)
247
+            l2 = r.x2;
248
+        else
249
+            r2 = r.x2;
250
+
251
+        int t1 = y1;
252
+        int b1 = y1;
253
+        if(y2 - y1 + 1 < 0)
254
+            t1 = y2;
255
+        else
256
+            b1 = y2;
257
+
258
+        int t2 = r.y1;
259
+        int b2 = r.y1;
260
+        if(r.y2 - r.y1 + 1 < 0)
261
+            t2 = r.y2;
262
+        else
263
+            b2 = r.y2;
264
+
265
+        TRect<T> tmp;
266
+        tmp.x1 = std::min(l1, l2);
267
+        tmp.x2 = std::max(r1, r2);
268
+        tmp.y1 = std::min(t1, t2);
269
+        tmp.y2 = std::max(b1, b2);
270
+        return tmp;
271
+    }
272
+
273
+    inline TRect<T>& operator=(const TRect<T>& other) { x1 = other.x1; y1 = other.y1; x2 = other.x2; y2 = other.y2; return *this;  }
274
+    inline bool operator==(const TRect<T>& other) const { return (x1 == other.x1 && y1 == other.y1 && x2 == other.x2 && y2 == other.y2); }
275
+    inline bool operator!=(const TRect<T>& other) const { return (x1 != other.x1 || y1 != other.y1 || x2 != other.x2 || y2 != other.y2); }
276
+
277
+private:
278
+    T x1, y1, x2, y2;
279
+};
280
+
281
+typedef TRect<int> Rect;
282
+typedef TRect<float> RectF;
283
+
284
+#endif // RECT_H

+ 0
- 1
src/resourcemanager.cpp View File

@@ -23,7 +23,6 @@
23 23
 
24 24
 
25 25
 #include "resourcemanager.h"
26
-#include "logger.h"
27 26
 
28 27
 #include <physfs.h>
29 28
 

+ 1
- 1
src/resourcemanager.h View File

@@ -25,7 +25,7 @@
25 25
 #ifndef RESOURCEMANAGER_H
26 26
 #define RESOURCEMANAGER_H
27 27
 
28
-#include <string>
28
+#include "prerequisites.h"
29 29
 
30 30
 class ResourceManager
31 31
 {

+ 113
- 0
src/size.h View File

@@ -0,0 +1,113 @@
1
+/* The MIT License
2
+ *
3
+ * Copyright (c) 2010 OTClient, https://github.com/edubart/otclient
4
+ *
5
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ * of this software and associated documentation files (the "Software"), to deal
7
+ * in the Software without restriction, including without limitation the rights
8
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ * copies of the Software, and to permit persons to whom the Software is
10
+ * furnished to do so, subject to the following conditions:
11
+ *
12
+ * The above copyright notice and this permission notice shall be included in
13
+ * all copies or substantial portions of the Software.
14
+ *
15
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ * THE SOFTWARE.
22
+ */
23
+
24
+
25
+#ifndef SIZE_H
26
+#define SIZE_H
27
+
28
+#include "prerequisites.h"
29
+#include "point.h"
30
+
31
+enum ESizeScaleMode {
32
+    IGNORE_ASPECT_RATIO,
33
+    KEEP_ASPECT_RATIO,
34
+    KEEP_ASPECT_RATIO_BY_EXPANDING
35
+};
36
+
37
+template <class T>
38
+class TSize
39
+{
40
+public:
41
+    TSize() : wd(-1), ht(-1) {};
42
+    TSize(T width, T height) : wd(width), ht(height) { };
43
+    TSize(const TSize<T>& other) : wd(other.wd), ht(other.ht) { };
44
+
45
+    inline TPoint<T> toPoint() const { return TPoint<T>(wd, ht); }
46
+
47
+    inline bool isNull() const { return wd==0 && ht==0; }
48
+    inline bool isEmpty() const { return wd<1 || ht<1; }
49
+    inline bool isValid() const { return wd>=0 && ht>=0; }
50
+
51
+    inline int width() const { return wd; }
52
+    inline int height() const { return ht; }
53
+
54
+    inline void setWidth(T w) { wd = w; }
55
+    inline void setHeight(T h) { ht = h; }
56
+
57
+    inline TSize<T> operator-() const { return TSize<T>(-wd, -ht); }
58
+    inline TSize<T> operator+(const TSize<T>& other) const { return TSize<T>(wd + other.wd, ht + other.ht);   }
59
+    inline TSize<T>& operator+=(const TSize<T>& other) { wd+=other.wd; ht+=other.ht; return *this; }
60
+    inline TSize<T> operator-(const TSize<T>& other) const { return TSize<T>(wd - other.wd, ht - other.ht);   }
61
+    inline TSize<T>& operator-=(const TSize<T>& other) { wd-=other.wd; ht-=other.ht; return *this; }
62
+    inline TSize<T> operator*(const float v) const { return TSize<T>((T)v*wd, (T)ht*v);  }
63
+    inline TSize<T>& operator*=(const float v) { wd=(T)v*wd; ht=(T)ht*v; return *this; }
64
+    inline TSize<T> operator/(const float v) const { return TSize<T>((T)wd/v, (T)ht/v);  }
65
+    inline TSize<T>& operator/=(const float v) { (T)wd/=v; (T)ht/=v; return *this; }
66
+
67
+    inline bool operator<=(const TSize<T>&other) const { return wd<=other.wd || ht<=other.ht; }
68
+    inline bool operator>=(const TSize<T>&other) const { return wd>=other.wd || ht>=other.ht; }
69
+    inline bool operator<(const TSize<T>&other) const { return wd<other.wd || ht<other.ht; }
70
+    inline bool operator>(const TSize<T>&other) const { return wd>other.wd || ht>other.ht; }
71
+
72
+    inline TSize<T>& operator=(const TSize<T>& other) { wd = other.wd; ht = other.ht; return *this; }
73
+    inline bool operator==(const TSize<T>& other) const { return other.wd==wd && other.ht==ht; }
74
+    inline bool operator!=(const TSize<T>& other) const { return other.wd!=wd || other.ht!=ht; }
75
+
76
+    inline TSize<T> expandedTo(const TSize<T>& other) const { return TSize<T>(std::max(wd,other.wd), std::max(ht,other.ht)); }
77
+    inline TSize<T> boundedTo(const TSize<T>& other) const { return TSize<T>(std::min(wd,other.wd), std::min(ht,other.ht)); }
78
+
79
+    void scale(const TSize<T>& s, ESizeScaleMode mode) {
80
+        if(mode == IGNORE_ASPECT_RATIO || wd == 0 || ht == 0) {
81
+            wd = s.wd;
82
+            ht = s.ht;
83
+        } else {
84
+            bool useHeight;
85
+            T rw = (s.ht * wd) / ht;
86
+
87
+            if(mode == KEEP_ASPECT_RATIO)
88
+                useHeight = (rw <= s.wd);
89
+            else // mode == KEEP_ASPECT_RATIO_BY_EXPANDING
90
+                useHeight = (rw >= s.wd);
91
+
92
+            if(useHeight) {
93
+                wd = rw;
94
+                ht = s.ht;
95
+            } else {
96
+                ht = (s.wd * ht)/wd;
97
+                wd = s.wd;
98
+            }
99
+        }
100
+    }
101
+    void scale(int w, int h, ESizeScaleMode mode) { scale(TSize<T>(w, h)); }
102
+
103
+    inline float ratio() const { return (float)wd/ht; }
104
+    inline T area() const { return wd*ht; }
105
+
106
+private:
107
+    T wd, ht;
108
+};
109
+
110
+typedef TSize<int> Size;
111
+typedef TSize<float> SizeF;
112
+
113
+#endif

+ 2
- 26
src/texture.cpp View File

@@ -24,13 +24,10 @@
24 24
 
25 25
 #include "texture.h"
26 26
 
27
-#include <GL/gl.h>
28
-#include <GL/glu.h>
29
-
30 27
 Texture::Texture(int width, int height, int components, unsigned char *pixels)
31 28
 {
32
-    m_width = width;
33
-    m_height = height;
29
+    m_size.setWidth(width);
30
+    m_size.setHeight(height);
34 31
 
35 32
     glGenTextures(1, &m_textureId);
36 33
     glBindTexture(GL_TEXTURE_2D, m_textureId);
@@ -66,30 +63,9 @@ Texture::~Texture()
66 63
         glDeleteTextures(1, &m_textureId);
67 64
 }
68 65
 
69
-void Texture::bind()
70
-{
71
-    glBindTexture(GL_TEXTURE_2D, m_textureId);
72
-}
73
-
74 66
 void Texture::enableBilinearFilter()
75 67
 {
76 68
     glBindTexture(GL_TEXTURE_2D, m_textureId);
77 69
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
78 70
     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
79 71
 }
80
-
81
-void Texture::draw(int x, int y)
82
-{
83
-    draw(x, y, m_width, m_height);
84
-}
85
-
86
-void Texture::draw(int x, int y, int width, int height)
87
-{
88
-    glBindTexture(GL_TEXTURE_2D, m_textureId);
89
-    glBegin(GL_QUADS);
90
-    glTexCoord2i(0, 0); glVertex2i(x,       y);
91
-    glTexCoord2i(0, 1); glVertex2i(x,       y+height);
92
-    glTexCoord2i(1, 1); glVertex2i(x+width, y+height);
93
-    glTexCoord2i(1, 0); glVertex2i(x+width, y);
94
-    glEnd();
95
-}

+ 5
- 11
src/texture.h View File

@@ -25,7 +25,8 @@
25 25
 #ifndef TEXTURE_H
26 26
 #define TEXTURE_H
27 27
 
28
-#include <GL/gl.h>
28
+#include "prerequisites.h"
29
+#include "size.h"
29 30
 #include <boost/shared_ptr.hpp>
30 31
 
31 32
 class TextureManager;
@@ -37,22 +38,15 @@ public:
37 38
     Texture(int width, int height, int components, unsigned char *pixels = NULL);
38 39
     virtual ~Texture();
39 40
 
40
-    /// Bind texture for drawing
41
-    void bind();
42
-
43 41
     /// Enable texture bilinear filter (smooth scaled textures)
44 42
     void enableBilinearFilter();
45 43
 
46
-    void draw(int x, int y);
47
-    void draw(int x, int y, int width, int height);
48
-
49
-    int getWidth() const { return m_width; }
50
-    int getHeight() const { return m_height; }
44
+    const Size& getSize() const { return m_size; }
45
+    GLuint getTextureId() const { return m_textureId; }
51 46
 
52 47
 private:
53 48
     GLuint m_textureId;
54
-    int m_width;
55
-    int m_height;
49
+    Size m_size;
56 50
 };
57 51
 
58 52
 typedef boost::shared_ptr<Texture> TexturePtr;

+ 10
- 11
src/textureloader.cpp View File

@@ -23,38 +23,37 @@
23 23
 
24 24
 
25 25
 #include "textureloader.h"
26
+#include "texture.h"
26 27
 
27
-#include <cstdio>
28 28
 #include <png.h>
29 29
 
30
-TexturePtr TextureLoader::loadPNG(const unsigned char *fileData, unsigned int fileSize)
30
+Texture *TextureLoader::loadPNG(const unsigned char *fileData, unsigned int fileSize)
31 31
 {
32
-    TexturePtr texture;
33 32
     FILE *pngFile = fmemopen((void*)fileData, fileSize, "rb");
34 33
 
35 34
     if(!pngFile)
36
-        return texture;
35
+        return NULL;
37 36
 
38 37
     png_byte sig[8];
39 38
     if(!fread(&sig, 8, 1, pngFile))
40
-        return texture;
39
+        return NULL;
41 40
 
42 41
     if(png_sig_cmp(sig, 0, 8))
43
-        return texture;
42
+        return NULL;
44 43
 
45 44
     png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
46 45
     if(!png_ptr)
47
-        return texture;
46
+        return NULL;
48 47
 
49 48
     png_infop info_ptr = png_create_info_struct(png_ptr);
50 49
     if(!info_ptr) {
51 50
         png_destroy_read_struct(&png_ptr, NULL, NULL);
52
-        return texture;
51
+        return NULL;
53 52
     }
54 53
 
55 54
     if(setjmp(png_jmpbuf(png_ptr))) {
56 55
         png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
57
-        return texture;
56
+        return NULL;
58 57
     }
59 58
 
60 59
     png_init_io(png_ptr, pngFile);
@@ -101,7 +100,7 @@ TexturePtr TextureLoader::loadPNG(const unsigned char *fileData, unsigned int fi
101 100
         default:
102 101
             if(png_ptr)
103 102
                 png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
104
-            return texture;
103
+            return NULL;
105 104
     };
106 105
 
107 106
     unsigned char *pixels = new unsigned char[width * height * components];
@@ -117,7 +116,7 @@ TexturePtr TextureLoader::loadPNG(const unsigned char *fileData, unsigned int fi
117 116
     fclose(pngFile);
118 117
     delete[] row_pointers;
119 118
 
120
-    texture = TexturePtr(new Texture(width, height, components, pixels));
119
+    Texture *texture = new Texture(width, height, components, pixels);
121 120
 
122 121
     delete[] pixels;
123 122
 

+ 4
- 2
src/textureloader.h View File

@@ -25,12 +25,14 @@
25 25
 #ifndef TEXTURELOADER_H
26 26
 #define TEXTURELOADER_H
27 27
 
28
-#include "texture.h"
28
+#include "prerequisites.h"
29
+
30
+class Texture;
29 31
 
30 32
 namespace TextureLoader
31 33
 {
32 34
     /// Load a png textures using libpng
33
-    TexturePtr loadPNG(const unsigned char *fileData, unsigned int fileSize);
35
+    Texture *loadPNG(const unsigned char *fileData, unsigned int fileSize);
34 36
 }
35 37
 
36 38
 #endif // TEXTURELOADER_H

+ 1
- 2
src/texturemanager.cpp View File

@@ -25,7 +25,6 @@
25 25
 #include "texturemanager.h"
26 26
 #include "resourcemanager.h"
27 27
 #include "textureloader.h"
28
-#include "logger.h"
29 28
 
30 29
 #include <boost/algorithm/string.hpp>
31 30
 
@@ -61,7 +60,7 @@ TexturePtr TextureManager::get(const std::string& textureFile)
61 60
         if(!textureFileData)
62 61
             return texture;
63 62
 
64
-        texture = TextureLoader::loadPNG(textureFileData, fileSize);
63
+        texture = TexturePtr(TextureLoader::loadPNG(textureFileData, fileSize));
65 64
         if(!texture)
66 65
             error("Unable to load texture %s, loading error.", textureFile.c_str());
67 66
         delete[] textureFileData;

+ 1
- 3
src/texturemanager.h View File

@@ -25,11 +25,9 @@
25 25
 #ifndef TEXTUREMANAGER_H
26 26
 #define TEXTUREMANAGER_H
27 27
 
28
+#include "prerequisites.h"
28 29
 #include "texture.h"
29 30
 
30
-#include <string>
31
-#include <map>
32
-
33 31
 class TextureManager
34 32
 {
35 33
 public:

+ 0
- 2
src/util.cpp View File

@@ -24,8 +24,6 @@
24 24
 
25 25
 #include "util.h"
26 26
 
27
-#include <cstdio>
28
-
29 27
 std::string vformat(const char *format, va_list args)
30 28
 {
31 29
     if(!format)

+ 1
- 3
src/util.h View File

@@ -25,9 +25,7 @@
25 25
 #ifndef UTIL_H
26 26
 #define UTIL_H
27 27
 
28
-#include <string>
29
-#include <sstream>
30
-#include <cstdarg>
28
+#include "prerequisites.h"
31 29
 
32 30
 /// Formatting like printf for std::string, va_list input version
33 31
 std::string vformat(const char *format, va_list args);

+ 0
- 8
src/x11platform.cpp View File

@@ -26,19 +26,11 @@
26 26
 #include "engine.h"
27 27
 #include "input.h"
28 28
 #include "logger.h"
29
-#include "const.h"
30 29
 
31
-#include <time.h>
32 30
 #include <sys/time.h>
33 31
 #include <sys/stat.h>
34 32
 #include <errno.h>
35 33
 
36
-#include <cstring>
37
-#include <string>
38
-#include <sstream>
39
-#include <algorithm>
40
-#include <map>
41
-
42 34
 #include <X11/Xlib.h>
43 35
 #include <X11/Xatom.h>
44 36
 #include <GL/glx.h>

Loading…
Cancel
Save