diff options
author | Luke Bratch <l_bratch@yahoo.co.uk> | 2010-11-25 15:04:54 +0000 |
---|---|---|
committer | Luke Bratch <l_bratch@yahoo.co.uk> | 2010-11-25 15:04:54 +0000 |
commit | 71926d883a678be983a434f8c0da435178d7585d (patch) | |
tree | 0d3598d060ae153aa129eb9eeddeb66f0c48717a | |
parent | 3383e8be9001826bf0d09f1fb73ad5013cc8627e (diff) |
Add Screen class, move video output functionality to it
-rw-r--r-- | Level.cpp | 2 | ||||
-rw-r--r-- | Level.h | 5 | ||||
-rw-r--r-- | Screen.cpp | 26 | ||||
-rw-r--r-- | Screen.h | 21 | ||||
-rw-r--r-- | intensemarcus.h | 5 | ||||
-rw-r--r-- | main.cpp | 17 |
6 files changed, 60 insertions, 16 deletions
@@ -31,6 +31,6 @@ void Level::move() { //std::cout << "Level::move(): Moving level items.\n"; } -void Level::draw(SDL_Surface *screen) { +void Level::draw(Screen *screen) { //std::cout << "Level::draw(): Drawing into the screen.\n"; } @@ -2,8 +2,7 @@ #define LEVEL_H #include "Player.h" -#include "SDL/SDL.h" -#include "SDL/SDL_image.h" +#include "Screen.h" class Level { public: @@ -16,7 +15,7 @@ class Level { void move(); // Draw the current scene on the screen - void draw(SDL_Surface *screen); + void draw(Screen *screen); // The human player Player player; diff --git a/Screen.cpp b/Screen.cpp new file mode 100644 index 0000000..5a0b157 --- /dev/null +++ b/Screen.cpp @@ -0,0 +1,26 @@ +#include <iostream> +#include "Screen.h" + +Screen::Screen() { + // Turn on the main screen + if (!(screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE))) + std::cout << "Error setting video mode.\n"; + + // Set window title + SDL_WM_SetCaption("Intense Marcus", NULL); +} + +void Screen::blit(int x, int y, SDL_Surface* src, SDL_Surface* dst, SDL_Rect* clip) { + // Destination position + SDL_Rect pos; + + pos.x = x; + pos.y = y; + + SDL_BlitSurface(src, clip, dst, &pos); +} + +void Screen::flip() { + if (SDL_Flip(screen) == -1) + std::cout << "Error flipping screen.\n"; +} diff --git a/Screen.h b/Screen.h new file mode 100644 index 0000000..c5672b5 --- /dev/null +++ b/Screen.h @@ -0,0 +1,21 @@ +#ifndef SCREEN_H +#define SCREEN_H + +#include "intensemarcus.h" +#include "SDL/SDL.h" + +class Screen { + public: + Screen(); + + /* blits src onto dst, at position x, y */ + void blit(int x, int y, SDL_Surface* src, SDL_Surface* dst, SDL_Rect* clip = NULL); + /* Swap screen buffers */ + void flip(); + + private: + // Game window + SDL_Surface *screen; +}; + +#endif diff --git a/intensemarcus.h b/intensemarcus.h index 7c3a330..3f7d5bf 100644 --- a/intensemarcus.h +++ b/intensemarcus.h @@ -1,4 +1,9 @@ +#ifndef INTENSEMARCUS_H +#define INTENSEMARCUS_H + const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 600; const int SCREEN_BPP = 32; const int FPS = 30; + +#endif @@ -9,8 +9,6 @@ int main(int argc, char* args[]) { // Main SDL event structure SDL_Event event; - // Ggame window - SDL_Surface *screen; // Current level number int levelNo = 1; // Main loop continues while true @@ -18,17 +16,13 @@ int main(int argc, char* args[]) { // Timer start Uint32 startTicks = 0; + // Game window + Screen screen; + // Initialise SDL if (SDL_Init(SDL_INIT_EVERYTHING) == -1) return 1; - // Turn on the main screen - if (!(screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE))) - return 1; - - // Set window title - SDL_WM_SetCaption("Intense Marcus", NULL); - Level level; level.changeLevel(levelNo); @@ -50,11 +44,10 @@ int main(int argc, char* args[]) { level.move(); // Draw level - level.draw(screen); + level.draw(&screen); // Update screen - if (SDL_Flip(screen) == -1) - return 1; + screen.flip(); // Set FPS cap to 30 if ((SDL_GetTicks() - startTicks) < 1000 / FPS) |