Separate SDL from graphics code

Also remove OS X specific project files and update Makefile to ensure
the Element class generator only runs when necessary
This commit is contained in:
Simon Robertshaw 2012-05-30 12:32:58 +01:00
parent 7074036b89
commit 86746f38b0
8 changed files with 95 additions and 118 deletions

4
.gitignore vendored
View File

@ -14,5 +14,7 @@ Saves/*
generated/*
generate
Makefile.me
PowderToypp.xcodeproj/*
*.xcodeproj
.DS_Store
*.plist
*.lproj

View File

@ -1,14 +1,9 @@
HEADERS := $(wildcard src/*.h) $(wildcard src/*/*.h) $(wildcard generated/*.h)
SOURCES := $(wildcard src/*.cpp) $(wildcard src/*/*.cpp) $(wildcard src/*/*/*.cpp) $(wildcard generated/*.cpp)
GENERATEDSOURCES := $(wildcard src/*/*/*.h) $(wildcard src/*/*/*.cpp)
OBJS := $(patsubst src/%.cpp,build/obj/%.o,$(SOURCES))
NEWLINE := $(`echo "d\nd"`)
ELEMENTFILES := $(patsubst src/simulation/%,\#include "%"$(NEWLINE),$(wildcard src/simulation/elements/*.cpp))
#ELEMENTFILES := \#include "elements/watr.cpp"
FOLDERS :=
CFLAGS := -w -Isrc/ -Idata/ -Igenerated/
OFLAGS := -fkeep-inline-functions
@ -73,7 +68,8 @@ buildpaths-powder-x:
$(shell mkdir -p build/obj/powder-x/)
$(shell mkdir -p $(sort $(dir $(patsubst build/obj/%.o,build/obj/powder-x/%.o,$(OBJS)))))
generate:
generate: $(GENERATEDSOURCES)
touch generate
python generator.py
clean:

File diff suppressed because one or more lines are too long

View File

@ -117,9 +117,7 @@ public:
#ifdef OGLR
//OpenGL specific instance variables
GLuint vidBuf, textTexture;
#else
SDL_Surface * sdl_scrn;
#endif
#endif
//Common graphics methods in Graphics.cpp
static char * GenerateGradient(pixel * colours, float * points, int pointcount, int size);
@ -147,8 +145,7 @@ public:
void draw_icon(int x, int y, Icon icon);
void Clear();
void AttachSDLSurface(SDL_Surface * surface);
void Blit();
void Finalise();
//
int drawtext(int x, int y, const char *s, int r, int g, int b, int a);
int drawtext(int x, int y, std::string s, int r, int g, int b, int a);

View File

@ -6,7 +6,45 @@
Graphics::Graphics():
sdl_scale(1)
{
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(0, (XRES+BARSIZE)*sdl_scale, 0, (YRES+MENUSIZE)*sdl_scale, -1, 1);
glOrtho(0, (XRES+BARSIZE)*sdl_scale, (YRES+MENUSIZE)*sdl_scale, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//glRasterPos2i(0, (YRES+MENUSIZE));
glRasterPos2i(0, 0);
glPixelZoom(1, 1);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//Texture for main UI
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &vidBuf);
glBindTexture(GL_TEXTURE_2D, vidBuf);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, XRES+BARSIZE, YRES+MENUSIZE, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
glGenTextures(1, &textTexture);
glBindTexture(GL_TEXTURE_2D, textTexture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
}
Graphics::~Graphics()
@ -19,72 +57,9 @@ void Graphics::Clear()
glClear(GL_COLOR_BUFFER_BIT);
}
void Graphics::AttachSDLSurface(SDL_Surface * surface)
void Graphics::Finalise()
{
//sdl_scrn = surface;
SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
//glOrtho(0, (XRES+BARSIZE)*sdl_scale, 0, (YRES+MENUSIZE)*sdl_scale, -1, 1);
glOrtho(0, (XRES+BARSIZE)*sdl_scale, (YRES+MENUSIZE)*sdl_scale, 0, -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
//glRasterPos2i(0, (YRES+MENUSIZE));
glRasterPos2i(0, 0);
glPixelZoom(1, 1);
glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
//Texture for main UI
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &vidBuf);
glBindTexture(GL_TEXTURE_2D, vidBuf);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, XRES+BARSIZE, YRES+MENUSIZE, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glBindTexture(GL_TEXTURE_2D, 0);
glGenTextures(1, &textTexture);
glBindTexture(GL_TEXTURE_2D, textTexture);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glBindTexture(GL_TEXTURE_2D, 0);
glDisable(GL_TEXTURE_2D);
}
void Graphics::Blit()
{
//glDrawPixels(w,h,GL_BGRA,GL_UNSIGNED_BYTE,src); //Why does this still think it's ABGR?
/*glEnable( GL_TEXTURE_2D );
glBindTexture(GL_TEXTURE_2D, vidBuf);
glColor4f(1.0f, 1.0f, 1.0f, 1.0f);
glTexSubImage2D(GL_TEXTURE_2D, 0, 0, 0, XRES+BARSIZE, YRES+MENUSIZE, GL_BGRA, GL_UNSIGNED_BYTE, vid);
glBegin(GL_QUADS);
glTexCoord2d(1, 0);
glVertex3f((XRES+BARSIZE)*sdl_scale, (YRES+MENUSIZE)*sdl_scale, 1.0);
glTexCoord2d(0, 0);
glVertex3f(0, (YRES+MENUSIZE)*sdl_scale, 1.0);
glTexCoord2d(0, 1);
glVertex3f(0, 0, 1.0);
glTexCoord2d(1, 1);
glVertex3f((XRES+BARSIZE)*sdl_scale, 0, 1.0);
glEnd();
glDisable( GL_TEXTURE_2D );*/
glFlush();
SDL_GL_SwapBuffers ();
}
int Graphics::drawtext(int x, int y, const char *s, int r, int g, int b, int a)

View File

@ -33,6 +33,38 @@ using namespace std;
extern "C" IMAGE_DOS_HEADER __ImageBase;
#endif
SDL_Surface * sdl_scrn;
#ifdef OGLR
void blit()
{
SDL_GL_SwapBuffers();
}
#else
void blit(pixel * vid)
{
if(sdl_scrn)
{
pixel * dst;
pixel * src = vid;
int j, x = 0, y = 0, w = XRES+BARSIZE, h = YRES+MENUSIZE, pitch = XRES+BARSIZE;
if (SDL_MUSTLOCK(sdl_scrn))
if (SDL_LockSurface(sdl_scrn)<0)
return;
dst=(pixel *)sdl_scrn->pixels+y*sdl_scrn->pitch/PIXELSIZE+x;
for (j=0; j<h; j++)
{
memcpy(dst, src, w*PIXELSIZE);
dst+=sdl_scrn->pitch/PIXELSIZE;
src+=pitch;
}
if (SDL_MUSTLOCK(sdl_scrn))
SDL_UnlockSurface(sdl_scrn);
SDL_UpdateRect(sdl_scrn,0,0,0,0);
}
#endif
}
SDL_Surface * SDLOpen()
{
SDL_Surface * surface;
@ -93,26 +125,18 @@ SDL_Surface * SDLOpen()
return surface;
}
/*int SDLPoll(SDL_Event * event)
{
while (SDL_PollEvent(event))
{
switch (event->type)
{
case SDL_QUIT:
return 1;
}
}
return 0;
}*/
int main(int argc, char * argv[])
{
int elapsedTime = 0, currentTime = 0, lastTime = 0, currentFrame = 0;
float fps = 0, delta = 1.0f;
sdl_scrn = SDLOpen();
#ifdef OGLR
SDL_GL_SetAttribute (SDL_GL_DOUBLEBUFFER, 1);
#endif
ui::Engine::Ref().g = new Graphics();
ui::Engine::Ref().g->AttachSDLSurface(SDLOpen());
//ui::Engine::Ref().g->AttachSDLSurface(SDLOpen());
ui::Engine * engine = &ui::Engine::Ref();
engine->Begin(XRES+BARSIZE, YRES+MENUSIZE);
@ -166,6 +190,12 @@ int main(int argc, char * argv[])
engine->Tick();
engine->Draw();
#ifdef OGLR
blit();
#else
blit(engine->g->vid);
#endif
currentFrame++;
currentTime = SDL_GetTicks();

View File

@ -20,32 +20,9 @@ void Graphics::Clear()
memset(vid, 0, PIXELSIZE * ((XRES+BARSIZE) * (YRES+MENUSIZE)));
}
void Graphics::AttachSDLSurface(SDL_Surface * surface)
void Graphics::Finalise()
{
sdl_scrn = surface;
}
void Graphics::Blit()
{
if(sdl_scrn)
{
pixel * dst;
pixel * src = vid;
int j, x = 0, y = 0, w = XRES+BARSIZE, h = YRES+MENUSIZE, pitch = XRES+BARSIZE;
if (SDL_MUSTLOCK(sdl_scrn))
if (SDL_LockSurface(sdl_scrn)<0)
return;
dst=(pixel *)sdl_scrn->pixels+y*sdl_scrn->pitch/PIXELSIZE+x;
for (j=0; j<h; j++)
{
memcpy(dst, src, w*PIXELSIZE);
dst+=sdl_scrn->pitch/PIXELSIZE;
src+=pitch;
}
if (SDL_MUSTLOCK(sdl_scrn))
SDL_UnlockSurface(sdl_scrn);
SDL_UpdateRect(sdl_scrn,0,0,0,0);
}
}
int Graphics::drawtext(int x, int y, const char *s, int r, int g, int b, int a)

View File

@ -184,7 +184,7 @@ void Engine::Draw()
char fpsText[512];
sprintf(fpsText, "FPS: %.2f, Delta: %.3f", fps, dt);
ui::Engine::Ref().g->drawtext(10, 10, fpsText, 255, 255, 255, 255);
g->Blit();
g->Finalise();
}
void Engine::SetFps(float fps)