瀏覽代碼

Change the Video class into an interface, implemented by SdlVideo and,

soon, GtkVideo.
legacy
Sam Hocevar sam 15 年之前
父節點
當前提交
5e9cb74861
共有 5 個檔案被更改,包括 63 行新增33 行删除
  1. +9
    -4
      src/Makefile
  2. +13
    -13
      src/sdlvideo.cpp
  3. +30
    -0
      src/sdlvideo.h
  4. +2
    -2
      src/test-map.cpp
  5. +9
    -14
      src/video.h

+ 9
- 4
src/Makefile 查看文件

@@ -1,11 +1,16 @@


SRC = test-map.cpp \
game.cpp video.cpp tiler.cpp tileset.cpp scene.cpp \
font.cpp layer.cpp map.cpp
COMMON = test-map.cpp \
game.cpp tiler.cpp tileset.cpp scene.cpp \
font.cpp layer.cpp map.cpp joystick.cpp
TEST_SRC = test-map.cpp sdlvideo.cpp $(COMMON)
EDITOR_SRC = editor.cpp gtkvideo.cpp $(COMMON)


all: test-map all: test-map


test-map: $(SRC:%.cpp=%.o)
test-map: $(TEST_SRC:%.cpp=%.o)
g++ -g -Wall -O3 $^ -o $@ `pkg-config --libs sdl gl SDL_image`

editor-map: $(EDITOR_SRC:%.cpp=%.o)
g++ -g -Wall -O3 $^ -o $@ `pkg-config --libs sdl gl SDL_image` g++ -g -Wall -O3 $^ -o $@ `pkg-config --libs sdl gl SDL_image`


%.o: %.cpp %.o: %.cpp


src/video.cpp → src/sdlvideo.cpp 查看文件

@@ -12,15 +12,15 @@


#include <SDL.h> #include <SDL.h>


#include "video.h"
#include "sdlvideo.h"


/* /*
* Video implementation class
* SDL Video implementation class
*/ */


class VideoData
class SdlVideoData
{ {
friend class Video;
friend class SdlVideo;


private: private:
SDL_Surface *video; SDL_Surface *video;
@@ -29,12 +29,12 @@ private:
}; };


/* /*
* Public Video class
* Public SdlVideo class
*/ */


Video::Video(char const *title, int width, int height)
SdlVideo::SdlVideo(char const *title, int width, int height)
{ {
data = new VideoData();
data = new SdlVideoData();


/* Initialise SDL */ /* Initialise SDL */
if (SDL_Init(SDL_INIT_VIDEO) < 0) if (SDL_Init(SDL_INIT_VIDEO) < 0)
@@ -79,23 +79,23 @@ Video::Video(char const *title, int width, int height)
data->frames = 0; data->frames = 0;
} }


int Video::GetWidth() const
int SdlVideo::GetWidth() const
{ {
return data->video->w; return data->video->w;
} }


int Video::GetHeight() const
int SdlVideo::GetHeight() const
{ {
return data->video->h; return data->video->h;
} }


void Video::Clear()
void SdlVideo::Clear()
{ {
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
glLoadIdentity(); glLoadIdentity();
} }


void Video::Refresh(float milliseconds)
void SdlVideo::Refresh(float milliseconds)
{ {
if (milliseconds > 0.0f) if (milliseconds > 0.0f)
while (SDL_GetTicks() < data->ticks + (milliseconds - 0.5f)) while (SDL_GetTicks() < data->ticks + (milliseconds - 0.5f))
@@ -106,12 +106,12 @@ void Video::Refresh(float milliseconds)
SDL_GL_SwapBuffers(); SDL_GL_SwapBuffers();
} }


void Video::FullScreen()
void SdlVideo::FullScreen()
{ {
SDL_WM_ToggleFullScreen(data->video); SDL_WM_ToggleFullScreen(data->video);
} }


Video::~Video()
SdlVideo::~SdlVideo()
{ {
Uint32 total = SDL_GetTicks() - data->start; Uint32 total = SDL_GetTicks() - data->start;
printf("%f fps\n", 1000.0f * data->frames / total); printf("%f fps\n", 1000.0f * data->frames / total);

+ 30
- 0
src/sdlvideo.h 查看文件

@@ -0,0 +1,30 @@

/*
* The video driver
*/

#if !defined __DH_SDLVIDEO_H__
#define __DH_SDLVIDEO_H__

#include "video.h"

class SdlVideoData;

class SdlVideo : public Video
{
public:
SdlVideo(char const *title, int width, int height);
virtual ~SdlVideo();

virtual int GetWidth() const;
virtual int GetHeight() const;
virtual void Clear();
virtual void Refresh(float milliseconds);
virtual void FullScreen();

private:
SdlVideoData *data;
};

#endif // __DH_SDLVIDEO_H__


+ 2
- 2
src/test-map.cpp 查看文件

@@ -5,12 +5,12 @@
#include <stdio.h> #include <stdio.h>
#include <math.h> #include <math.h>


#include "video.h"
#include "sdlvideo.h"
#include "game.h" #include "game.h"


int main(int argc, char **argv) int main(int argc, char **argv)
{ {
Video *video = new Video("Deus Hax", 640, 480);
Video *video = new SdlVideo("Deus Hax", 640, 480);
Game *game = new Game("maps/testmap-library.tmx"); Game *game = new Game("maps/testmap-library.tmx");


for (int done = 0; !done; ) for (int done = 0; !done; )


+ 9
- 14
src/video.h 查看文件

@@ -1,27 +1,22 @@


/* /*
* The video driver
* The video interface
*/ */


#if !defined __DH_VIDEO_H__ #if !defined __DH_VIDEO_H__
#define __DH_VIDEO_H__ #define __DH_VIDEO_H__


class VideoData;

class Video class Video
{ {
public: public:
Video(char const *title, int width, int height);
~Video();

int GetWidth() const;
int GetHeight() const;
void Clear();
void Refresh(float milliseconds);
void FullScreen();

private:
VideoData *data;
//Video(char const *title, int width, int height);
//virtual ~Video();

virtual int GetWidth() const = 0;
virtual int GetHeight() const = 0;
virtual void Clear() = 0;
virtual void Refresh(float milliseconds) = 0;
virtual void FullScreen() = 0;
}; };


#endif // __DH_VIDEO_H__ #endif // __DH_VIDEO_H__


Loading…
取消
儲存