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/* generated/*
generate generate
Makefile.me Makefile.me
PowderToypp.xcodeproj/* *.xcodeproj
.DS_Store .DS_Store
*.plist
*.lproj

View File

@ -1,14 +1,9 @@
HEADERS := $(wildcard src/*.h) $(wildcard src/*/*.h) $(wildcard generated/*.h) HEADERS := $(wildcard src/*.h) $(wildcard src/*/*.h) $(wildcard generated/*.h)
SOURCES := $(wildcard src/*.cpp) $(wildcard src/*/*.cpp) $(wildcard src/*/*/*.cpp) $(wildcard generated/*.cpp) 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)) 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/ CFLAGS := -w -Isrc/ -Idata/ -Igenerated/
OFLAGS := -fkeep-inline-functions OFLAGS := -fkeep-inline-functions
@ -73,7 +68,8 @@ buildpaths-powder-x:
$(shell mkdir -p build/obj/powder-x/) $(shell mkdir -p build/obj/powder-x/)
$(shell mkdir -p $(sort $(dir $(patsubst build/obj/%.o,build/obj/powder-x/%.o,$(OBJS))))) $(shell mkdir -p $(sort $(dir $(patsubst build/obj/%.o,build/obj/powder-x/%.o,$(OBJS)))))
generate: generate: $(GENERATEDSOURCES)
touch generate
python generator.py python generator.py
clean: clean:

File diff suppressed because one or more lines are too long

View File

@ -117,9 +117,7 @@ public:
#ifdef OGLR #ifdef OGLR
//OpenGL specific instance variables //OpenGL specific instance variables
GLuint vidBuf, textTexture; GLuint vidBuf, textTexture;
#else #endif
SDL_Surface * sdl_scrn;
#endif
//Common graphics methods in Graphics.cpp //Common graphics methods in Graphics.cpp
static char * GenerateGradient(pixel * colours, float * points, int pointcount, int size); 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 draw_icon(int x, int y, Icon icon);
void Clear(); void Clear();
void AttachSDLSurface(SDL_Surface * surface); void Finalise();
void Blit();
// //
int drawtext(int x, int y, const char *s, int r, int g, int b, int a); 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); 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(): Graphics::Graphics():
sdl_scale(1) 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() Graphics::~Graphics()
@ -19,72 +57,9 @@ void Graphics::Clear()
glClear(GL_COLOR_BUFFER_BIT); 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(); glFlush();
SDL_GL_SwapBuffers ();
} }
int Graphics::drawtext(int x, int y, const char *s, int r, int g, int b, int a) 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; extern "C" IMAGE_DOS_HEADER __ImageBase;
#endif #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 * SDLOpen()
{ {
SDL_Surface * surface; SDL_Surface * surface;
@ -93,26 +125,18 @@ SDL_Surface * SDLOpen()
return surface; 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 main(int argc, char * argv[])
{ {
int elapsedTime = 0, currentTime = 0, lastTime = 0, currentFrame = 0; int elapsedTime = 0, currentTime = 0, lastTime = 0, currentFrame = 0;
float fps = 0, delta = 1.0f; 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 = new Graphics();
ui::Engine::Ref().g->AttachSDLSurface(SDLOpen()); //ui::Engine::Ref().g->AttachSDLSurface(SDLOpen());
ui::Engine * engine = &ui::Engine::Ref(); ui::Engine * engine = &ui::Engine::Ref();
engine->Begin(XRES+BARSIZE, YRES+MENUSIZE); engine->Begin(XRES+BARSIZE, YRES+MENUSIZE);
@ -166,6 +190,12 @@ int main(int argc, char * argv[])
engine->Tick(); engine->Tick();
engine->Draw(); engine->Draw();
#ifdef OGLR
blit();
#else
blit(engine->g->vid);
#endif
currentFrame++; currentFrame++;
currentTime = SDL_GetTicks(); currentTime = SDL_GetTicks();

View File

@ -20,32 +20,9 @@ void Graphics::Clear()
memset(vid, 0, PIXELSIZE * ((XRES+BARSIZE) * (YRES+MENUSIZE))); 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) 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]; char fpsText[512];
sprintf(fpsText, "FPS: %.2f, Delta: %.3f", fps, dt); sprintf(fpsText, "FPS: %.2f, Delta: %.3f", fps, dt);
ui::Engine::Ref().g->drawtext(10, 10, fpsText, 255, 255, 255, 255); ui::Engine::Ref().g->drawtext(10, 10, fpsText, 255, 255, 255, 255);
g->Blit(); g->Finalise();
} }
void Engine::SetFps(float fps) void Engine::SetFps(float fps)