From 413302330666a135389b759139ff9d5b6eb20052 Mon Sep 17 00:00:00 2001 From: Luke Bratch Date: Thu, 25 Nov 2010 20:35:18 +0000 Subject: Add background to Level, add sample background, modify Screen::blit() to always blit onto the game window --- Level.cpp | 6 ++++++ Level.h | 3 ++- Screen.cpp | 4 ++-- Screen.h | 4 ++-- intensemarcus.cpp | 24 ++++++++++++++++++++++++ intensemarcus.h | 6 ++++++ space.png | Bin 0 -> 970896 bytes 7 files changed, 42 insertions(+), 5 deletions(-) create mode 100644 intensemarcus.cpp create mode 100644 space.png diff --git a/Level.cpp b/Level.cpp index 27a77be..0dec3ef 100644 --- a/Level.cpp +++ b/Level.cpp @@ -25,6 +25,10 @@ void Level::changeLevel(int levelNo) { // Checking where the player is for fun/demonstration purposes std::cout << "Level::changeLevel(): player is at " << player.getX() << ", " << player.getY() << ".\n"; + + // Load background image + if (!(background = loadImage("space.png"))) + std::cout << "Level::changeLevel: Error loading background image.\n"; } void Level::move() { @@ -33,4 +37,6 @@ void Level::move() { void Level::draw(Screen *screen) { //std::cout << "Level::draw(): Drawing into the screen.\n"; + // Blit background + screen->blit(0, 0, background); } diff --git a/Level.h b/Level.h index a56b734..c65eab9 100644 --- a/Level.h +++ b/Level.h @@ -21,7 +21,8 @@ class Level { Player player; private: - // Nothing yet! + // Background image + SDL_Surface *background; }; #endif diff --git a/Screen.cpp b/Screen.cpp index 5a0b157..1a0e6cb 100644 --- a/Screen.cpp +++ b/Screen.cpp @@ -10,14 +10,14 @@ Screen::Screen() { SDL_WM_SetCaption("Intense Marcus", NULL); } -void Screen::blit(int x, int y, SDL_Surface* src, SDL_Surface* dst, SDL_Rect* clip) { +void Screen::blit(int x, int y, SDL_Surface* src, SDL_Rect* clip) { // Destination position SDL_Rect pos; pos.x = x; pos.y = y; - SDL_BlitSurface(src, clip, dst, &pos); + SDL_BlitSurface(src, clip, screen, &pos); } void Screen::flip() { diff --git a/Screen.h b/Screen.h index c5672b5..e535f0f 100644 --- a/Screen.h +++ b/Screen.h @@ -8,8 +8,8 @@ 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); + /* blits src onto screen, at position x, y */ + void blit(int x, int y, SDL_Surface* src, SDL_Rect* clip = NULL); /* Swap screen buffers */ void flip(); diff --git a/intensemarcus.cpp b/intensemarcus.cpp new file mode 100644 index 0000000..c866a1a --- /dev/null +++ b/intensemarcus.cpp @@ -0,0 +1,24 @@ +#include "intensemarcus.h" + +/* SDL_Surface loads an image from a file, and + converts it into the same format as the + display, for faster blitting. */ +SDL_Surface *loadImage(std::string filename) { + // Image as it is loaded + SDL_Surface* raw = NULL; + + // Image converted into same format as display + SDL_Surface* image = NULL; + + raw = IMG_Load(filename.c_str()); + + if(raw != NULL) { + // Convert the image, using the PNG's alpha channel + image = SDL_DisplayFormatAlpha(raw); + + // Free raw image + SDL_FreeSurface(raw); + } + + return image; +} diff --git a/intensemarcus.h b/intensemarcus.h index 3f7d5bf..543eafc 100644 --- a/intensemarcus.h +++ b/intensemarcus.h @@ -1,9 +1,15 @@ #ifndef INTENSEMARCUS_H #define INTENSEMARCUS_H +#include "SDL/SDL.h" +#include "SDL/SDL_image.h" +#include + const int SCREEN_WIDTH = 800; const int SCREEN_HEIGHT = 600; const int SCREEN_BPP = 32; const int FPS = 30; +SDL_Surface *loadImage(std::string filename); + #endif diff --git a/space.png b/space.png new file mode 100644 index 0000000..6305927 Binary files /dev/null and b/space.png differ -- cgit v1.2.3