Skip to content

Commit

Permalink
Use SdlSurface everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
Flamefire committed Jan 3, 2020
1 parent d74766a commit c2d6afb
Show file tree
Hide file tree
Showing 22 changed files with 135 additions and 165 deletions.
7 changes: 5 additions & 2 deletions CGame_Render.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ void CGame::SetAppIcon()

void CGame::Render()
{
if(Extent(Surf_Display->w, Surf_Display->h) != GameResolution || fullscreen != ((Surf_Display->flags & SDL_FULLSCREEN) != 0) || useOpenGL != CSurface::useOpenGL)
if(Extent(Surf_Display->w, Surf_Display->h) != GameResolution || fullscreen != ((Surf_Display->flags & SDL_FULLSCREEN) != 0)
|| useOpenGL != CSurface::useOpenGL)
{
ReCreateWindow();
}
Expand All @@ -50,8 +51,10 @@ void CGame::Render()
surfLoadScreen->h - 1);

if(useOpenGL)
{
SDL_BlitSurface(Surf_Display.get(), nullptr, Surf_DisplayGL.get(), nullptr);
SDL_GL_SwapBuffers();
else
} else
SDL_Flip(Surf_Display.get());
return;
}
Expand Down
14 changes: 2 additions & 12 deletions CIO/CButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,9 @@ CButton::CButton(void callback(int), int clickedParam, Sint16 x, Sint16 y, Uint1
this->clickedParam = clickedParam;
motionEntryParam = -1;
motionLeaveParam = -1;
Surf_Button = nullptr;
needSurface = true;
needRender = true;
}

CButton::~CButton()
{
SDL_FreeSurface(Surf_Button);
}

void CButton::setButtonPicture(int picture)
{
this->button_picture = picture;
Expand Down Expand Up @@ -150,13 +143,10 @@ bool CButton::render()
return true;
needRender = false;
// if we need a new surface
if(needSurface)
if(!Surf_Button)
{
SDL_FreeSurface(Surf_Button);
Surf_Button = nullptr;
if((Surf_Button = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, 0, 0, 0, 0)) == nullptr)
if((Surf_Button = makeSdlSurface(SDL_SWSURFACE, w, h, 32)) == nullptr)
return false;
needSurface = false;
}

// at first completly fill the background (not the fastest way, but simplier)
Expand Down
11 changes: 4 additions & 7 deletions CIO/CButton.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
#ifndef _CBUTTON_H
#define _CBUTTON_H

#include "../defines.h"
#include "SDL.h"
#include "defines.h"
#include "SdlSurface.h"

class CButton
{
friend class CDebug;

private:
SDL_Surface* Surf_Button;
bool needSurface;
SdlSurface Surf_Button;
bool needRender;
Sint16 x_;
Sint16 y_;
Expand All @@ -30,10 +29,8 @@ class CButton
int motionLeaveParam;

public:
// Constructor - Destructor
CButton(void callback(int), int clickedParam, Sint16 x = 0, Sint16 y = 0, Uint16 w = 20, Uint16 h = 20, int color = BUTTON_GREY,
const char* text = nullptr, int button_picture = -1);
~CButton();
// Access
int getX() { return x_; };
int getY() { return y_; };
Expand All @@ -49,7 +46,7 @@ class CButton
SDL_Surface* getSurface()
{
render();
return Surf_Button;
return Surf_Button.get();
};
void setColor(int color);
void setTextColor(int color)
Expand Down
2 changes: 1 addition & 1 deletion CIO/CControlContainer.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#ifndef _CCONTROLCONTAINER_H
#define _CCONTROLCONTAINER_H

#include "../defines.h"
#include "defines.h"
#include <memory>
#include <vector>

Expand Down
12 changes: 6 additions & 6 deletions CIO/CFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -435,7 +435,7 @@ bool CFile::open_lbm(const std::string& filename)
CHECK_READ(libendian::be_read_ui(&length, fp));

// now we are ready to read the picture lines and fill the surface, so lets create one
if(!(bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, bmpArray->w, bmpArray->h, 8, 0, 0, 0, 0)))
if(!(bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, bmpArray->w, bmpArray->h, 8)))
return false;
SDL_SetPalette(bmpArray->surface.get(), SDL_LOGPAL, colors.data(), 0, colors.size());

Expand Down Expand Up @@ -504,7 +504,7 @@ bool CFile::open_lbm(const std::string& filename)
SDL_SetColorKey(bmpArray->surface.get(), SDL_SRCCOLORKEY, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0));

bmpArray++;
if((bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, (bmpArray - 1)->w, (bmpArray - 1)->h, 32, 0, 0, 0, 0)))
if((bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, (bmpArray - 1)->w, (bmpArray - 1)->h, 32)))
{
SDL_SetColorKey(bmpArray->surface.get(), SDL_SRCCOLORKEY, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0));
CSurface::Draw(bmpArray->surface, (bmpArray - 1)->surface, 0, 0);
Expand Down Expand Up @@ -1054,7 +1054,7 @@ bool CFile::read_bob02()
CHECK_READ(libendian::le_read_us(&starts[y], fp));

// now we are ready to read the picture lines and fill the surface, so lets create one
if((bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, bmpArray->w, bmpArray->h, 8, 0, 0, 0, 0)) == nullptr)
if((bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, bmpArray->w, bmpArray->h, 8)) == nullptr)
return false;
SDL_SetPalette(bmpArray->surface.get(), SDL_LOGPAL, palActual->colors.data(), 0, palActual->colors.size());
SDL_SetColorKey(bmpArray->surface.get(), SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(bmpArray->surface->format, 0, 0, 0));
Expand Down Expand Up @@ -1224,7 +1224,7 @@ bool CFile::read_bob04(int player_color)
CHECK_READ(libendian::le_read_us(&starts[y], fp));

// now we are ready to read the picture lines and fill the surface, so lets create one
if((bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, bmpArray->w, bmpArray->h, 8, 0, 0, 0, 0)) == nullptr)
if((bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, bmpArray->w, bmpArray->h, 8)) == nullptr)
return false;

SDL_SetPalette(bmpArray->surface.get(), SDL_LOGPAL, palActual->colors.data(), 0, palActual->colors.size());
Expand Down Expand Up @@ -1363,7 +1363,7 @@ bool CFile::read_bob07()
CHECK_READ(libendian::le_read_us(&starts[y], fp));

// now we are ready to read the picture lines and fill the surface, so lets create one
if((shadowArray->surface = makeSdlSurface(SDL_SWSURFACE, shadowArray->w, shadowArray->h, 8, 0, 0, 0, 0)) == nullptr)
if((shadowArray->surface = makeSdlSurface(SDL_SWSURFACE, shadowArray->w, shadowArray->h, 8)) == nullptr)
return false;
SDL_SetPalette(shadowArray->surface.get(), SDL_LOGPAL, palActual->colors.data(), 0, palActual->colors.size());
// SDL_SetAlpha(shadowArray->surface, SDL_SRCALPHA, 128);
Expand Down Expand Up @@ -1472,7 +1472,7 @@ bool CFile::read_bob14()
return true;

// now we are ready to read the picture lines and fill the surface, so lets create one
if((bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, bmpArray->w, bmpArray->h, 8, 0, 0, 0, 0)) == nullptr)
if((bmpArray->surface = makeSdlSurface(SDL_SWSURFACE, bmpArray->w, bmpArray->h, 8)) == nullptr)
return false;
SDL_SetPalette(bmpArray->surface.get(), SDL_LOGPAL, palActual->colors.data(), 0, palActual->colors.size());

Expand Down
2 changes: 1 addition & 1 deletion CIO/CFile.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
#ifndef _CFILE_H
#define _CFILE_H

#include "../defines.h"
#include "defines.h"
#include <cstdio>
#include <string>

Expand Down
52 changes: 30 additions & 22 deletions CIO/CFont.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
#include "../globals.h"
#include <cassert>

CFont::CFont(std::string text, unsigned x, unsigned y, unsigned fontsize, unsigned color)
: Surf_Font(nullptr), x_(x), y_(y), string_(std::move(text))
CFont::CFont(std::string text, unsigned x, unsigned y, unsigned fontsize, unsigned color) : x_(x), y_(y), string_(std::move(text))
{
// only three sizes are available (in pixels)
if(fontsize != 9 && fontsize != 11 && fontsize != 14)
Expand All @@ -14,38 +13,42 @@ CFont::CFont(std::string text, unsigned x, unsigned y, unsigned fontsize, unsign
initialColor_ = this->color_ = color;
callback = nullptr;
clickedParam = 0;
// create surface and write text to it
writeText();
}

CFont::~CFont()
void CFont::setPos(Position pos)
{
SDL_FreeSurface(Surf_Font);
if(pos != Position(x_, y_))
{
x_ = pos.x;
y_ = pos.y;
Surf_Font.reset();
}
}

void CFont::setFontsize(unsigned fontsize)
{
if(fontsize != 9 && fontsize != 11 && fontsize != 14)
this->fontsize_ = 9;
else
this->fontsize_ = fontsize;
writeText();
fontsize = 9;
if(fontsize != fontsize_)
{
fontsize_ = fontsize;
Surf_Font.reset();
}
}

void CFont::setColor(unsigned color)
{
if(color != color_)
Surf_Font.reset();
initialColor_ = color_ = color;
writeText();
}

void CFont::setText(std::string text)
{
if(text == string_)
return;
SDL_FreeSurface(Surf_Font);
Surf_Font = nullptr;
Surf_Font.reset();
this->string_ = std::move(text);
writeText();
}

void CFont::setMouseData(SDL_MouseButtonEvent button)
Expand All @@ -72,6 +75,13 @@ void CFont::setMouseData(SDL_MouseButtonEvent button)
}
}

SDL_Surface* CFont::getSurface()
{
if(!Surf_Font)
writeText();
return Surf_Font.get();
}

namespace {
unsigned getIndexForChar(uint8_t c)
{
Expand Down Expand Up @@ -201,10 +211,10 @@ unsigned getCharWidth(uint8_t c, unsigned fontsize, unsigned color)
}
} // namespace

bool CFont::writeText()
void CFont::writeText()
{
if(string_.empty())
return true;
return;
// data for counting pixels to create the surface
unsigned pixel_ctr_w = 0;
unsigned pixel_ctr_w_tmp = 0;
Expand Down Expand Up @@ -244,11 +254,10 @@ bool CFont::writeText()
pixel_ctr_w = pixel_ctr_w_tmp;
w = pixel_ctr_w;
h = pixel_ctr_h;
if(Surf_Font)
SDL_FreeSurface(Surf_Font);
if(!(Surf_Font = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, 0, 0, 0, 0)))
return false;
SDL_SetColorKey(Surf_Font, SDL_SRCCOLORKEY, SDL_MapRGB(Surf_Font->format, 0, 0, 0));
Surf_Font = makeSdlSurface(SDL_SWSURFACE, w, h, 32);
if(!Surf_Font)
return;
SDL_SetColorKey(Surf_Font.get(), SDL_SRCCOLORKEY, SDL_MapRGB(Surf_Font->format, 0, 0, 0));
chiffre = string_.begin();
pixel_count_loop = false;
continue;
Expand Down Expand Up @@ -286,7 +295,6 @@ bool CFont::writeText()
// go to next chiffre
++chiffre;
}
return true;
}

bool CFont::writeText(SDL_Surface* Surf_Dest, const std::string& string, unsigned x, unsigned y, unsigned fontsize, unsigned color,
Expand Down
15 changes: 7 additions & 8 deletions CIO/CFont.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
#ifndef _CFONT_H
#define _CFONT_H

#include "../defines.h"
#include "defines.h"
#include <SDL.h>
#include <functional>
#include <memory>
#include <string>

class CFont
{
friend class CDebug;

private:
SDL_Surface* Surf_Font;
SdlSurface Surf_Font;
Sint16 x_;
Sint16 y_;
Uint16 w;
Expand All @@ -22,15 +23,14 @@ class CFont
std::function<void(int)> callback;
int clickedParam;

void writeText();

public:
// Constructor - Destructor
CFont(std::string text, unsigned x = 0, unsigned y = 0, unsigned fontsize = 9, unsigned color = FONT_YELLOW);
~CFont();
// Access
int getX() { return x_; };
int getY() { return y_; };
void setX(int x) { this->x_ = x; };
void setY(int y) { this->y_ = y; };
void setPos(Position pos);
unsigned getW() { return w; };
unsigned getH() { return fontsize_; };
void setFontsize(unsigned fontsize);
Expand All @@ -48,10 +48,9 @@ class CFont
clickedParam = 0;
}
void setMouseData(SDL_MouseButtonEvent button);
SDL_Surface* getSurface() { return Surf_Font; };
SDL_Surface* getSurface();
// Methods
// fontsize can be 9, 11 or 14 (otherwise it will be set to 9) ---- '\n' is possible
bool writeText();
// this function can be used as CFont::writeText to write text directly to a surface without creating an object
static bool writeText(SDL_Surface* Surf_Dest, const std::string& string, unsigned x = 0, unsigned y = 0, unsigned fontsize = 9,
unsigned color = FONT_YELLOW, FontAlign align = FontAlign::Left);
Expand Down
15 changes: 3 additions & 12 deletions CIO/CPicture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,9 @@ CPicture::CPicture(void callback(int), int clickedParam, Uint16 x, Uint16 y, int
this->clickedParam = clickedParam;
motionEntryParam = -1;
motionLeaveParam = -1;
Surf_Picture = nullptr;
needSurface = true;
needRender = true;
}

CPicture::~CPicture()
{
SDL_FreeSurface(Surf_Picture);
}

void CPicture::setMouseData(const SDL_MouseMotionEvent& motion)
{
// cursor is on the picture
Expand Down Expand Up @@ -77,14 +70,12 @@ bool CPicture::render()
return true;
needRender = false;
// if we need a new surface
if(needSurface)
if(!Surf_Picture)
{
SDL_FreeSurface(Surf_Picture);
Surf_Picture = SDL_CreateRGBSurface(SDL_SWSURFACE, w, h, 32, 0, 0, 0, 0);
Surf_Picture = makeSdlSurface(SDL_SWSURFACE, w, h, 32);
if(!Surf_Picture)
return false;
SDL_SetColorKey(Surf_Picture, SDL_SRCCOLORKEY, SDL_MapRGB(Surf_Picture->format, 0, 0, 0));
needSurface = false;
SDL_SetColorKey(Surf_Picture.get(), SDL_SRCCOLORKEY, SDL_MapRGB(Surf_Picture->format, 0, 0, 0));
}

CSurface::Draw(Surf_Picture, global::bmpArray[picture_].surface, 0, 0);
Expand Down
Loading

0 comments on commit c2d6afb

Please sign in to comment.