diff options
-rw-r--r-- | Level.cpp | 6 | ||||
-rw-r--r-- | Level.h | 3 | ||||
-rw-r--r-- | Screen.cpp | 4 | ||||
-rw-r--r-- | Screen.h | 4 | ||||
-rw-r--r-- | intensemarcus.cpp | 24 | ||||
-rw-r--r-- | intensemarcus.h | 6 | ||||
-rw-r--r-- | space.png | bin | 0 -> 970896 bytes |
7 files changed, 42 insertions, 5 deletions
@@ -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); } @@ -21,7 +21,8 @@ class Level { Player player; private: - // Nothing yet! + // Background image + SDL_Surface *background; }; #endif @@ -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() { @@ -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 <string> + 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 Binary files differnew file mode 100644 index 0000000..6305927 --- /dev/null +++ b/space.png |