Skip to content

Commit 32e9b5c

Browse files
Draw terrain using OpenGL
1 parent d20d5ca commit 32e9b5c

36 files changed

Lines changed: 555 additions & 9681 deletions

CGame.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ class CGame
4949
Texture cursor_;
5050
Texture cursorClicked_;
5151
Texture cross_;
52-
Texture mapTex_; ///< Texture for the map/terrain (Surf_Map may be 8-bit)
5352

5453
// structure for mouse cursor
5554
struct

CGame_Render.cpp

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -55,28 +55,44 @@ void CGame::Render()
5555
// render the map if active
5656
if(MapObj && MapObj->isActive())
5757
{
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-
}
58+
// Set up map-space projection: (displayRect.left, top) maps to (0,0) screen
59+
auto viewRect = MapObj->getDisplayRect();
60+
glMatrixMode(GL_PROJECTION);
61+
glPushMatrix();
62+
glLoadIdentity();
63+
glOrtho(static_cast<GLdouble>(viewRect.left), static_cast<GLdouble>(viewRect.left + GameResolution.x),
64+
static_cast<GLdouble>(viewRect.top + GameResolution.y), static_cast<GLdouble>(viewRect.top), -1, 1);
65+
glMatrixMode(GL_MODELVIEW);
66+
glPushMatrix();
67+
glLoadIdentity();
68+
69+
MapObj->render();
70+
71+
// Restore screen-space projection
72+
glMatrixMode(GL_PROJECTION);
73+
glPopMatrix();
74+
glMatrixMode(GL_MODELVIEW);
75+
glPopMatrix();
76+
77+
// HUD text overlays drawn directly via OpenGL
78+
std::array<char, 100> textBuffer;
79+
// text for x and y of vertex (shown in upper left corner)
80+
std::snprintf(textBuffer.data(), textBuffer.size(), "%d %d", MapObj->getVertexX(), MapObj->getVertexY());
81+
CFont::draw(textBuffer.data(), Position(20, 20), FontSize::Medium);
82+
// text for MinReduceHeight and MaxRaiseHeight
83+
std::snprintf(textBuffer.data(), textBuffer.size(),
84+
"min. height: %#04x/0x3C max. height: %#04x/0x3C NormalNull: 0x0A",
85+
MapObj->getMinReduceHeight(), MapObj->getMaxRaiseHeight());
86+
CFont::draw(textBuffer.data(), Position(100, 20), FontSize::Medium);
87+
// text for MovementLocked
88+
if(MapObj->isHorizontalMovementLocked() && MapObj->isVerticalMovementLocked())
89+
CFont::draw("Movement locked (F9 or F10 to unlock)", Position(20, 40), FontSize::Large, FontColor::Orange);
90+
else if(MapObj->isHorizontalMovementLocked())
91+
CFont::draw("Horizontal movement locked (F9 to unlock)", Position(20, 40), FontSize::Large,
92+
FontColor::Orange);
93+
else if(MapObj->isVerticalMovementLocked())
94+
CFont::draw("Vertical movement locked (F10 to unlock)", Position(20, 40), FontSize::Large,
95+
FontColor::Orange);
8096
}
8197

8298
// render active menus

CIO/CFile.cpp

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -470,20 +470,10 @@ bool CFile::read_lbm(FILE* fp, const boost::filesystem::path& filepath)
470470
} else
471471
return false;
472472

473-
// if this is a texture file, we need a secondary 32-bit surface for SGE and we set a color key at both surfaces
473+
// if this is a texture file, set a color key for transparency
474474
if(filepath.filename() == "TEX5.LBM" || filepath.filename() == "TEX6.LBM" || filepath.filename() == "TEX7.LBM"
475475
|| filepath.filename() == "TEXTUR_0.LBM" || filepath.filename() == "TEXTUR_3.LBM")
476-
{
477-
SDL_SetColorKey(bmpArray->surface.get(), SDL_TRUE, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0));
478-
479-
bmpArray++;
480-
if((bmpArray->surface = makeRGBSurface((bmpArray - 1)->w, (bmpArray - 1)->h)))
481-
{
482-
SDL_SetColorKey(bmpArray->surface.get(), SDL_TRUE, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0));
483-
CSurface::Draw(bmpArray->surface, (bmpArray - 1)->surface);
484-
} else
485-
bmpArray--;
486-
}
476+
SDL_SetColorKey(bmpArray->surface.get(), SDL_TRUE, 0);
487477

488478
// we are finished, the surface is filled
489479
// increment bmpArray for the next picture
@@ -1013,7 +1003,7 @@ bool CFile::read_bob02(FILE* fp)
10131003
// now we are ready to read the picture lines and fill the surface, so lets create one
10141004
if((bmpArray->surface = makePalSurface(bmpArray->w, bmpArray->h, palActual->colors)) == nullptr)
10151005
return false;
1016-
SDL_SetColorKey(bmpArray->surface.get(), SDL_TRUE, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0));
1006+
SDL_SetColorKey(bmpArray->surface.get(), SDL_TRUE, 0);
10171007
// SDL_SetAlpha(bmpArray->surface, SDL_SRCALPHA, 128);
10181008

10191009
// main loop for reading picture lines
@@ -1174,7 +1164,7 @@ bool CFile::read_bob04(FILE* fp, int player_color)
11741164
if((bmpArray->surface = makePalSurface(bmpArray->w, bmpArray->h, palActual->colors)) == nullptr)
11751165
return false;
11761166

1177-
SDL_SetColorKey(bmpArray->surface.get(), SDL_TRUE, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0));
1167+
SDL_SetColorKey(bmpArray->surface.get(), SDL_TRUE, 0);
11781168

11791169
// main loop for reading picture lines
11801170
for(Position pos{0, 0}; pos.y < bmpArray->h; pos.y++)

CIO/CFont.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -190,13 +190,6 @@ unsigned getCharWidth(uint8_t c, FontSize fontsize, FontColor color)
190190
}
191191
} // namespace
192192

193-
void CFont::draw(Position parentOrigin) const
194-
{
195-
if(string_.empty())
196-
return;
197-
draw(string_, parentOrigin + pos_, fontsize_, color_, FontAlign::Left);
198-
}
199-
200193
void CFont::draw(const std::string& string, Position pos, FontSize fontsize, FontColor color, FontAlign align)
201194
{
202195
if(string.empty())
@@ -301,7 +294,10 @@ unsigned CFont::getTextWidth(const std::string& string, FontSize fontsize)
301294
{
302295
unsigned w = 0;
303296
for(unsigned char c : string)
297+
{
298+
if(c == '\n')
299+
break;
304300
w += getCharWidth(c, fontsize, FontColor::Yellow); // width is same for all colors
305-
301+
}
306302
return w;
307303
}

CIO/CFont.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ class CFont
4848
void setMouseData(SDL_MouseButtonEvent button);
4949

5050
/// Draw this font's text at the given absolute position
51-
void draw(Position parentOrigin) const;
51+
void draw(Position parentOrigin) const { draw(string_, parentOrigin + pos_, fontsize_, color_, FontAlign::Left); }
5252

5353
/// Draw text directly
5454
/// @param pos Absolute position (top-left of the text, adjusted for alignment).

CMakeLists.txt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,9 @@ cmake_minimum_required(VERSION 3.16..3.20)
88
project(s25edit)
99

1010
find_package(Boost 1.64 REQUIRED)
11+
find_package(SDL2 REQUIRED)
12+
1113

12-
add_subdirectory(SGE)
1314

1415
option(RTTR_EDITOR_ADMINMODE "In admin mode there are some key combos to open debugger, resource viewer and so on" OFF)
1516
if(RTTR_EDITOR_ADMINMODE)
@@ -38,7 +39,7 @@ ELSE()
3839
ENDIF()
3940

4041
add_executable(s25edit ${MAIN_SOURCES} ${CIO_SOURCES} ${icon_RC})
41-
target_link_libraries(s25edit PRIVATE SGE rttrConfig s25Common gamedata siedler2 endian::static glad Boost::nowide PUBLIC Boost::disable_autolinking Boost::program_options)
42+
target_link_libraries(s25edit PRIVATE rttrConfig s25Common gamedata siedler2 endian::static glad Boost::nowide SDL2::SDL2 PUBLIC Boost::disable_autolinking Boost::program_options)
4243
target_include_directories(s25edit PRIVATE include)
4344
target_compile_features(s25edit PRIVATE cxx_std_17)
4445

0 commit comments

Comments
 (0)