Skip to content

Commit d20d5ca

Browse files
authored
Merge pull request #52 from morganchristiansson/gl2-ui
OpenGL UI components
2 parents 4d34caa + 2a5ad4d commit d20d5ca

28 files changed

Lines changed: 637 additions & 1130 deletions

CGame.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,8 @@ int CGame::Execute()
7777

7878
void CGame::RenderPresent()
7979
{
80-
displayTexture_.upload(Surf_Display->pixels);
81-
displayTexture_.Draw(Rect(0, 0, GameResolution.x, GameResolution.y));
82-
8380
const auto& cursorImg = Cursor.clicked ? (Cursor.button.right ? cross_ : cursorClicked_) : cursor_;
84-
cursorImg.Draw(Cursor.pos);
81+
cursorImg.draw(Cursor.pos);
8582

8683
SDL_GL_SwapWindow(window_.get());
8784
}

CGame.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
#pragma once
77

88
#include "CIO/CFont.h"
9-
#include "SdlSurface.h"
109
#include "Texture.h"
1110
#include <boost/filesystem/path.hpp>
1211
#include <Point.h>
@@ -27,8 +26,6 @@ class CGame
2726

2827
bool Running;
2928
bool showLoadScreen;
30-
SdlSurface Surf_Display;
31-
Texture displayTexture_;
3229
SDL_GLContext glContext_ = nullptr;
3330
SdlWindow window_;
3431

@@ -52,6 +49,7 @@ class CGame
5249
Texture cursor_;
5350
Texture cursorClicked_;
5451
Texture cross_;
52+
Texture mapTex_; ///< Texture for the map/terrain (Surf_Map may be 8-bit)
5553

5654
// structure for mouse cursor
5755
struct
@@ -111,6 +109,5 @@ class CGame
111109
CMap* getMapObj();
112110
void delMapObj();
113111
void enterEditor(const boost::filesystem::path& filepath);
114-
SDL_Surface* getDisplaySurface() const { return Surf_Display.get(); };
115112
auto getRes() const { return GameResolution; }
116113
};

CGame_Event.cpp

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include "CIO/CWindow.h"
99
#include "CMap.h"
1010
#include "CSurface.h"
11+
#include "CollisionDetection.h"
1112
#include "callbacks.h"
1213
#include "globals.h"
1314

@@ -195,9 +196,7 @@ void CGame::EventHandling(SDL_Event* Event)
195196
if(!Window->isWaste() && Window->getPriority() == actualPriority)
196197
{
197198
// is the cursor INSIDE the window or does the user move or resize the window?
198-
if(((Event->motion.x >= Window->getX()) && (Event->motion.x < Window->getX() + Window->getW())
199-
&& (Event->motion.y >= Window->getY())
200-
&& (Event->motion.y < Window->getY() + Window->getH()))
199+
if(IsPointInRect(Event->motion.x, Event->motion.y, Rect(Window->getPos(), Window->getSize()))
201200
|| Window->isMoving() || Window->isResizing())
202201
{
203202
// Windows[i]->setActive();
@@ -275,9 +274,7 @@ void CGame::EventHandling(SDL_Event* Event)
275274
if(!Window->isWaste() && Window->getPriority() == actualPriority)
276275
{
277276
// is the cursor INSIDE the window?
278-
if((Event->button.x >= Window->getX()) && (Event->button.x < Window->getX() + Window->getW())
279-
&& (Event->button.y >= Window->getY())
280-
&& (Event->button.y < Window->getY() + Window->getH()))
277+
if(IsPointInRect(Event->button.x, Event->button.y, Rect(Window->getPos(), Window->getSize())))
281278
{
282279
Window->setActive();
283280
Window->setPriority(highestPriority + 1);
@@ -338,9 +335,7 @@ void CGame::EventHandling(SDL_Event* Event)
338335
if(!Window->isWaste() && Window->getPriority() == actualPriority)
339336
{
340337
// is the cursor INSIDE the window?
341-
if((Event->button.x >= Window->getX()) && (Event->button.x < Window->getX() + Window->getW())
342-
&& (Event->button.y >= Window->getY())
343-
&& (Event->button.y < Window->getY() + Window->getH()))
338+
if(IsPointInRect(Event->button.x, Event->button.y, Rect(Window->getPos(), Window->getSize())))
344339
{
345340
// Windows[i]->setActive();
346341
// Windows[i]->setPriority(highestPriority+1);

CGame_Init.cpp

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,6 @@ bool CGame::CreateWindow()
3838
SDL_ShowWindow(window_.get());
3939

4040
ApplyWindowChanges();
41-
if(!displayTexture_.isValid() || !Surf_Display)
42-
return false;
4341

4442
SetAppIcon();
4543

@@ -125,16 +123,7 @@ void CGame::UpdateDisplaySize(const Extent& newSize)
125123
appliedResolution_ = GameResolution;
126124
appliedFullscreen_ = fullscreen;
127125

128-
Surf_Display = makeRGBSurface(GameResolution.x, GameResolution.y, true);
129-
displayTexture_.createEmpty(GameResolution);
130-
131126
setGLViewport();
132-
for(auto& menu : Menus)
133-
{
134-
menu->resetSurface();
135-
}
136-
for(auto& wnd : Windows)
137-
wnd->resetSurface();
138127
}
139128

140129
bool CGame::Init()
@@ -182,7 +171,7 @@ bool CGame::Init()
182171
// std::cout << "\nShow loading screen...";
183172
showLoadScreen = true;
184173
glClear(GL_COLOR_BUFFER_BIT);
185-
splashBg_.Draw(Rect(0, 0, GameResolution.x, GameResolution.y));
174+
splashBg_.draw(Rect(0, 0, GameResolution.x, GameResolution.y));
186175
SDL_GL_SwapWindow(window_.get());
187176

188177
GameDataLoader gdLoader(global::worldDesc);

CGame_Render.cpp

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
#include "CIO/CWindow.h"
1010
#include "CMap.h"
1111
#include "CSurface.h"
12+
#include "Texture.h"
1213
#include "globals.h"
1314
#include <glad/glad.h>
1415
#ifdef _WIN32
@@ -40,46 +41,49 @@ void CGame::SetAppIcon()
4041
void CGame::Render()
4142
{
4243
glClear(GL_COLOR_BUFFER_BIT);
43-
SDL_FillRect(Surf_Display.get(), nullptr, SDL_MapRGBA(Surf_Display->format, 0, 0, 0, 0));
44+
glEnable(GL_BLEND);
45+
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
4446

4547
// if the S2 loading screen is shown, render only this until user clicks a mouse button
4648
if(showLoadScreen)
4749
{
48-
splashBg_.Draw(Rect(0, 0, GameResolution.x, GameResolution.y));
50+
splashBg_.draw(Rect(0, 0, GameResolution.x, GameResolution.y));
4951
SDL_GL_SwapWindow(window_.get());
5052
return;
5153
}
5254

5355
// render the map if active
5456
if(MapObj && MapObj->isActive())
5557
{
56-
CSurface::Draw(Surf_Display, MapObj->getSurface(), 0, 0);
57-
std::array<char, 100> textBuffer;
58-
// text for x and y of vertex (shown in upper left corner)
59-
std::snprintf(textBuffer.data(), textBuffer.size(), "%d %d", MapObj->getVertexX(), MapObj->getVertexY());
60-
CFont::writeText(Surf_Display, textBuffer.data(), Position(20, 20));
61-
// text for MinReduceHeight and MaxRaiseHeight
62-
std::snprintf(textBuffer.data(), textBuffer.size(),
63-
"min. height: %#04x/0x3C max. height: %#04x/0x3C NormalNull: 0x0A",
64-
MapObj->getMinReduceHeight(), MapObj->getMaxRaiseHeight());
65-
CFont::writeText(Surf_Display, textBuffer.data(), Position(100, 20));
66-
// text for MovementLocked
67-
if(MapObj->isHorizontalMovementLocked() && MapObj->isVerticalMovementLocked())
68-
CFont::writeText(Surf_Display, "Movement locked (F9 or F10 to unlock)", Position(20, 40), FontSize::Large,
69-
FontColor::Orange);
70-
else if(MapObj->isHorizontalMovementLocked())
71-
CFont::writeText(Surf_Display, "Horizontal movement locked (F9 to unlock)", Position(20, 40),
72-
FontSize::Large, FontColor::Orange);
73-
else if(MapObj->isVerticalMovementLocked())
74-
CFont::writeText(Surf_Display, "Vertical movement locked (F10 to unlock)", Position(20, 40),
75-
FontSize::Large, FontColor::Orange);
58+
if(auto* mapSurf = MapObj->getSurface())
59+
{
60+
std::array<char, 100> textBuffer;
61+
std::snprintf(textBuffer.data(), textBuffer.size(), "%d %d", MapObj->getVertexX(), MapObj->getVertexY());
62+
CFont::writeText(mapSurf, textBuffer.data(), 20, 20);
63+
std::snprintf(textBuffer.data(), textBuffer.size(),
64+
"min. height: %#04x/0x3C max. height: %#04x/0x3C NormalNull: 0x0A",
65+
MapObj->getMinReduceHeight(), MapObj->getMaxRaiseHeight());
66+
CFont::writeText(mapSurf, textBuffer.data(), 100, 20);
67+
if(MapObj->isHorizontalMovementLocked() && MapObj->isVerticalMovementLocked())
68+
CFont::writeText(mapSurf, "Movement locked (F9 or F10 to unlock)", 20, 40, FontSize::Large,
69+
FontColor::Orange);
70+
else if(MapObj->isHorizontalMovementLocked())
71+
CFont::writeText(mapSurf, "Horizontal movement locked (F9 to unlock)", 20, 40, FontSize::Large,
72+
FontColor::Orange);
73+
else if(MapObj->isVerticalMovementLocked())
74+
CFont::writeText(mapSurf, "Vertical movement locked (F10 to unlock)", 20, 40, FontSize::Large,
75+
FontColor::Orange);
76+
77+
mapTex_.load(mapSurf);
78+
mapTex_.draw(Rect(0, 0, GameResolution.x, GameResolution.y));
79+
}
7680
}
7781

7882
// render active menus
7983
for(auto& Menu : Menus)
8084
{
8185
if(Menu->isActive())
82-
CSurface::Draw(Surf_Display, Menu->getSurface(), 0, 0);
86+
Menu->draw(Position(0, 0));
8387
}
8488

8589
// render windows ordered by priority
@@ -96,7 +100,7 @@ void CGame::Render()
96100
for(auto& Window : Windows)
97101
{
98102
if(Window->getPriority() == actualPriority)
99-
CSurface::Draw(Surf_Display, Window->getSurface(), Window->getX(), Window->getY());
103+
Window->draw(Position(0, 0));
100104
}
101105
}
102106

@@ -105,7 +109,6 @@ void CGame::Render()
105109
#endif
106110

107111
++framesPassedSinceLastFps;
108-
109112
const auto curTicks = SDL_GetTicks();
110113
const auto diffTicks = curTicks - lastFpsTick;
111114
if(diffTicks > 1000)
@@ -114,9 +117,16 @@ void CGame::Render()
114117
framesPassedSinceLastFps = 0;
115118
lastFpsTick = curTicks;
116119
}
117-
CSurface::Draw(Surf_Display, lastFps.getSurface(), 0, 0);
120+
lastFps.draw(Position(0, 0));
121+
122+
const auto& cursorImg = Cursor.clicked ? (Cursor.button.right ? cross_ : cursorClicked_) : cursor_;
123+
cursorImg.draw(Cursor.pos);
118124

119-
RenderPresent();
125+
SDL_GL_SwapWindow(window_.get());
126+
127+
#ifdef _ADMINMODE
128+
FrameCounter++;
129+
#endif
120130

121131
if(msWait)
122132
SDL_Delay(msWait);

0 commit comments

Comments
 (0)