SDL: Image want load because of the following code?:

This is a discussion on SDL: Image want load because of the following code?: within the C++ Programming forums, part of the General Programming Boards category; Hi C++/SDL programmers! I'm reading the book SDL Game Development and everything where working fine until I came to this ...

  1. #1
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    91

    SDL: Image want load because of the following code?:

    Hi C++/SDL programmers! I'm reading the book SDL Game Development and everything where working fine until I came to this point when they wanted me to create many classes to make it as easy as possible to load a image onto the screen in one line of code.

    But I'm stuck with how to get the image to show onto the screen. I have checked that I'm using the correct path for the image in the directory serveral times and the extensions such as .png, .jpg and more . . .

    So I guess something is wrong with the code. One problem is that it is many serparate files and I don't know if this is the right place to ask someones to help me out with this.

    I'm using Code::Blocks with MinGW if somebody wonder about which compiler I'm using.

    This is how the image looks like that I'm trying to load:
    Name:  animate-alpha.png
Views: 269
Size:  8.2 KB

    If anyone responds to this. I'll like to thank them for their help!

    Here comes the code:

    Game.h
    Code:
    #include <iostream>
    #include <vector>
    #include "SDL.h"
    #include "TextureManager.h"
    #include "GameObject.h"
    #include "Player.h"
    
    
    using namespace std;
    
    
    #ifndef GAME_H_INCLUDED
    #define GAME_H_INCLUDED
    
    
    class Game
    {
        public:
            static Game* Instance()
            {
                if (s_pInstance == 0)
                {
                    s_pInstance = new Game();
                }
                return s_pInstance;
            }
    
    
            bool init(const char* title, int x, int y, int w, int h, int flags);
            void handleEvents();
            void draw();
            void update();
            void render();
            void clean();
    
    
            bool running() {return m_bRunning;}
    
    
            SDL_Renderer* getRenderer() const {return m_pRenderer;}
    
    
        private:
            Game() {}
            ~Game() {}
    
    
            static Game* s_pInstance;
    
    
            SDL_Window* m_pWindow;
            SDL_Renderer* m_pRenderer;
    
    
            bool m_bRunning;
    
    
            int m_currentFrame;
    
    
            vector<GameObject*> m_gameObjects;
    };
    
    
    typedef Game TheGame;
    
    
    #endif // GAME_H_INCLUDED
    Game.cpp
    Code:
    #include <iostream>
    #include <vector>
    #include "SDL.h"
    #include "SDL_image.h"
    #include "Game.h"
    #include "TextureManager.h"
    #include "Enemy.h"
    
    
    using namespace std;
    
    
    Game* Game::s_pInstance = 0;
    
    
    bool Game::init(const char* title, int x, int y, int w, int h, int flags)
    {
        // Init SDL
        if (SDL_Init(SDL_INIT_EVERYTHING) >= 0)
        {
            cout << "SDL init success!\n";
    
    
            // Init the window
            this->m_pWindow = SDL_CreateWindow(title, x, y, w, h, flags);
    
    
            if (this->m_pWindow != 0) // Window init success
            {
                cout << "Window creation success!\n";
                this->m_pRenderer = SDL_CreateRenderer(this->m_pWindow, -1, 0);
    
    
                if (this->m_pRenderer != 0) // Renderer init success
                {
                    cout << "Renderer creation success!\n";
                    SDL_SetRenderDrawColor(this->m_pRenderer, 0, 255, 0, 255);
    
    
                    TheTextureManager::Instance()->load("assets/animate-alpha.png", "animate", this->m_pRenderer);
                    this->m_gameObjects.push_back(new Player(new LoaderParams(100, 100, 128, 82, "animate")));
                    this->m_gameObjects.push_back(new Enemy(new LoaderParams(300, 300, 128, 82, "animate")));
                }
                else
                {
                    cout << "Renderer init fail!\n";
                    return false;
                }
            }
            else
            {
                cout << "Window init fail!\n";
                return false;
            }
        }
        else
        {
            cout << "SDL init fail!\n";
            return false;
        }
        cout << "Init success!\n";
        this->m_bRunning = true; // Everything initialize successfully. Start the main loop
        return true;
    }
    
    
    void Game::draw()
    {
    
    
    }
    
    
    void Game::render()
    {
        SDL_RenderClear(this->m_pRenderer);
        SDL_RenderPresent(this->m_pRenderer);
    }
    
    
    void Game::clean()
    {
        cout << "Cleaning game . . .\n";
        SDL_DestroyWindow(this->m_pWindow);
        SDL_DestroyRenderer(this->m_pRenderer);
        SDL_Quit();
    }
    
    
    void Game::handleEvents()
    {
        SDL_Event event;
        if (SDL_PollEvent(&event))
        {
            switch (event.type)
            {
                case SDL_QUIT: this->m_bRunning = false; break;
                default: break;
            }
        }
    }
    
    
    void Game::update()
    {
    
    
    }
    So this is the class for the "Game". The class that runs the program in the main.cpp file:

    main.cpp
    Code:
    #include <iostream>
    #include "SDL.h"
    
    
    #include "Game.h"
    #include "TextureManager.h"
    #include "Player.h"
    
    
    using namespace std;
    
    
    int main(int argc, char* argv[])
    {
        if (TheGame::Instance()->init("Fails to renderer?!", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_FOREIGN))
        {
            while (TheGame::Instance()->running())
            {
                TheGame::Instance()->handleEvents();
                TheGame::Instance()->update();
                TheGame::Instance()->render();
                SDL_Delay(10);
            }
            TheGame::Instance()->clean();
        }
        return 0;
    }
    The main.cpp file uses the functions of the Game.h class to make the program run.

    Then I needed something to load and draw images with so then the book learnt me how to create a texture manager.

    TextureManager.h
    Code:
    #include <iostream>
    #include <map>
    #include "SDL.h"
    
    
    using namespace std;
    
    
    #ifndef TEXTUREMANAGER_H_INCLUDED
    #define TEXTUREMANAGER_H_INCLUDED
    
    
    class TextureManager
    {
        public:
            static TextureManager* Instance()
            {
                if (s_pInstance == 0)
                {
                    s_pInstance = new TextureManager();
                }
                return s_pInstance;
            }
    
    
            bool load(string fileName, string id, SDL_Renderer* pRenderer);
            void draw(string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip);
            void drawFrame(string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip = SDL_FLIP_NONE);
    
    
        private:
            TextureManager() {}
            ~TextureManager() {}
            static TextureManager* s_pInstance;
    
    
            map<string, SDL_Texture*> m_textureMap;
    };
    
    
    typedef TextureManager TheTextureManager;
    
    
    #endif // TEXTUREMANAGER_H_INCLUDED
    TextureManager.cpp
    Code:
    #include <iostream>
    #include "SDL.h"
    #include "SDL_image.h"
    #include "TextureManager.h"
    
    
    using namespace std;
    
    
    TextureManager* TextureManager::s_pInstance = 0;
    
    
    bool TextureManager::load(string fileName, string id, SDL_Renderer* pRenderer)
    {
        SDL_Surface* pTempSurface = IMG_Load(fileName.c_str());
    
    
        if (pTempSurface == 0)
        {
            return false;
        }
    
    
        SDL_Texture* pTexture = SDL_CreateTextureFromSurface(pRenderer, pTempSurface);
    
    
        SDL_FreeSurface(pTempSurface);
    
    
        // Everything went ok. Add the texture to the list
        if (pTexture != 0)
        {
            this->m_textureMap[id] = pTexture;
            return true;
        }
        // If reaching here. Something went wrong
        return false;
    }
    
    
    void TextureManager::draw(string id, int x, int y, int width, int height, SDL_Renderer* pRenderer, SDL_RendererFlip flip)
    {
        SDL_Rect srcRect;
        SDL_Rect dstRect;
    
    
        srcRect.x = 0;
        srcRect.y = 0;
        srcRect.w = dstRect.w = width;
        srcRect.h = dstRect.h = height;
        dstRect.x = x;
        dstRect.y = y;
    
    
        SDL_RenderCopyEx(pRenderer, this->m_textureMap[id], &srcRect, &dstRect, 0, 0, flip);
    }
    
    
    void TextureManager::drawFrame(string id, int x, int y, int width, int height, int currentRow, int currentFrame, SDL_Renderer* pRenderer, SDL_RendererFlip flip)
    {
        SDL_Rect srcRect;
        SDL_Rect dstRect;
    
    
        srcRect.x = width * currentFrame;
        srcRect.y = height * (currentRow - 1);
        srcRect.w = dstRect.w = width;
        srcRect.h = dstRect.h = height;
        dstRect.x = x;
        dstRect.y = y;
    
    
        SDL_RenderCopyEx(pRenderer, this->m_textureMap[id], &srcRect, &dstRect, 0, 0, flip);
    }
    So this is the class that will have the functions to load a image and I used the load and draw functions from it earlier in the book and then it worked fine. It's just now when I'm going to seperate the code more to make it easier I don't get it to work

    The first thing that the book said I was going to do was a abstract GameObject class:
    Code:
    #include <iostream>
    #include "SDL.h"
    #include "LoaderParams.h"
    
    
    using namespace std;
    
    
    #ifndef GAMEOBJECT_H_INCLUDED
    #define GAMEOBJECT_H_INCLUDED
    
    
    // The abstract base class
    class GameObject
    {
        public:
            // Implement the base class functions
            virtual void draw()=0;
            virtual void update()=0;
            virtual void clean()=0;
    
    
        protected:
            GameObject(const LoaderParams* pParams) {}
            virtual ~GameObject() {}
    };
    
    
    #endif // GAMEOBJECT_H_INCLUDED
    If somebody wonders:
    GameObject.cpp
    Code:
    #include <iostream>
    #include "SDL.h"
    #include "TextureManager.h"
    #include "GameObject.h"
    
    
    using namespace std;
    And then create a derived class that inherits from the GameObject class:

    SDLGameObject.h
    Code:
    #include <iostream>
    #include "SDL.h"
    #include "LoaderParams.h"
    #include "GameObject.h"
    
    
    using namespace std;
    
    
    #ifndef SDLGAMEOBJECT_H_INCLUDED
    #define SDLGAMEOBJECT_H_INCLUDED
    
    
    // Create the class that will load the images onto the screen
    class SDLGameObject : public GameObject
    {
        public:
            SDLGameObject(const LoaderParams* pParams);
    
    
            virtual void draw();
            virtual void update();
            virtual void clean();
    
    
        protected:
    
    
            // Create the private member variables that is used to translate the image
            int m_x;
            int m_y;
    
    
            int m_width;
            int m_height;
    
    
            int m_currentRow;
            int m_currentFrame;
    
    
            string m_textureID;
    };
    
    
    #endif // SDLGAMEOBJECT_H_INCLUDED
    SDLGameObject.cpp
    Code:
    #include <iostream>
    #include "Game.h"
    #include "TextureManager.h"
    #include "SDLGameObject.h"
    
    
    using namespace std;
    The SDLGameObject functions are defined in a class called LoaderParams instead (I don't know why? But the book told me to define them there?):

    LoaderParams.h
    Code:
    #include <iostream>
    #include "SDL.h"
    
    
    using namespace std;
    
    
    #ifndef LOADERPARAMS_H_INCLUDED
    #define LOADERPARAMS_H_INCLUDED
    
    
    class LoaderParams
    {
        public:
            // Create a constructor that inherits the member variables
            LoaderParams(int x, int y, int width, int height, string textureID) : m_x(x), m_y(y), m_width(width), m_height(height), m_textureID(textureID) {}
    
    
            // Create the functions to access the private member variables set from the constructor
            int getX() const {return m_x;}
            int getY() const {return m_y;}
            int getWidth() const {return m_width;}
            int getHeight() const {return m_height;}
            string getTextureID() const {return m_textureID;}
    
    
        private:
            int m_x;
            int m_y;
    
    
            int m_width;
            int m_height;
    
    
            string m_textureID;
    };
    
    
    #endif // LOADERPARAMS_H_INCLUDED
    So it's in the .cpp file them are defined:
    LoaderParams.cpp
    Code:
    #include <iostream>
    #include "SDL.h"
    #include "Game.h"
    #include "TextureManager.h"
    #include "GameObject.h"
    #include "LoaderParams.h"
    #include "SDLGameObject.h"
    
    
    using namespace std;
    
    
    // Define the constructor and inherit the GameObject constructor and pass in the LoaderParams* pParams pointer variable
    SDLGameObject::SDLGameObject(const LoaderParams* pParams) : GameObject(pParams)
    {
        this->m_x = pParams->getX();
        this->m_y = pParams->getY();
    
    
        this->m_width = pParams->getWidth();
        this->m_height = pParams->getHeight();
    
    
        this->m_textureID = pParams->getTextureID();
    
    
        this->m_currentRow = 1;
        this->m_currentFrame = 1;
    }
    
    
    void SDLGameObject::draw()
    {
        // Get the TextureManager drawFrame() function to draw images onto the screen
        TextureManager::Instance()->drawFrame(this->m_textureID, this->m_x, this->m_y, this->m_width, this->m_height, this->m_currentRow, this->m_currentFrame, TheGame::Instance()->getRenderer(), SDL_FLIP_NONE);
    }
    
    
    void SDLGameObject::update()
    {
    
    
    }
    
    
    void SDLGameObject::clean()
    {
    
    
    }
    So now over to the important part of this. Here is the class for the player called Player

    Player.h
    Code:
    #include <iostream>
    #include "SDL.h"
    #include "LoaderParams.h"
    #include "SDLGameObject.h"
    
    
    using namespace std;
    
    
    #ifndef PLAYER_H_INCLUDED
    #define PLAYER_H_INCLUDED
    
    
    class Player : public SDLGameObject
    {
        public:
            Player(const LoaderParams* pParams);
    
    
            virtual void draw();
            virtual void update();
            virtual void clean();
    };
    
    
    #endif // PLAYER_H_INCLUDED
    Player.cpp
    Code:
    #include <iostream>
    #include "SDL.h"
    #include "SDLGameObject.h"
    #include "Player.h"
    
    
    using namespace std;
    
    
    // Define the construtor of the Player class to create the player
    Player::Player(const LoaderParams* pParams) : SDLGameObject(pParams)
    {
    
    
    }
    
    
    // Define the function that will draw the image onto the screen
    void Player::draw()
    {
        SDLGameObject::draw(); // Use the SDLGameObject draw function that includes the TextureManager draw function and the Game class renderer
    }
    
    
    void Player::update()
    {
        this->m_x -= 1;
        this->m_currentFrame = int(((SDL_GetTicks() / 100) % 6));
    }
    
    
    void Player::clean()
    {
    
    
    }

  2. #2
    Registered User MutantJohn's Avatar
    Join Date
    Feb 2013
    Posts
    1,012
    I know this isn't a helpful post but holy poop is that a lot of code O_o

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    91
    Haha yeah, It is! . But it would be even better if I could get it to work. The only problem with the code is that it won't load a image

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    you totally did not need to post all that code, you have 77 posts you should know that. Tell us what happens when you run the game - does it just show a black screen, what?
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  5. #5
    Registered User
    Join Date
    Nov 2012
    Posts
    1,053
    Quote Originally Posted by DecoratorFawn82 View Post
    Haha yeah, It is! . But it would be even better if I could get it to work. The only problem with the code is that it won't load a image
    Look at this line

    Code:
    SDL_Surface* pTempSurface = IMG_Load(fileName.c_str());
    What happens when IMG_Load fails? You're checking the return value in the function but you're not doing anything useful if it fails. I believe there's a function in the SDL API for telling you more about what went wrong with files kind of similar to how perror works in the standard library. Look in the header file where IMG_Load is defined.

  6. #6
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,409
    c99 _ Mate i like your post but this is code that has been wholesale copied and dumped without comprehension, call me a cynic
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  7. #7
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    91
    Okay, I will try to edit it and make it more clear what I'm creating

  8. #8
    Registered User
    Join Date
    Feb 2013
    Location
    Sweden
    Posts
    91
    But it would really help if someone has read the book: SDL Game Development and chapter 3 from page 62. That's where I got stuck.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My ini load code.
    By RMDan in forum C Programming
    Replies: 16
    Last Post: 06-25-2009, 12:07 AM
  2. load image from scanner
    By Colin in forum C++ Programming
    Replies: 2
    Last Post: 01-20-2003, 12:53 PM
  3. How to write a C code to load an image file.
    By dv007 in forum C Programming
    Replies: 4
    Last Post: 05-25-2002, 05:27 PM
  4. How-To: Load Pictures in MFC. Code Inside.
    By Xei in forum C++ Programming
    Replies: 1
    Last Post: 05-16-2002, 09:17 PM
  5. Load and run machine code at runtime
    By ninebit in forum C++ Programming
    Replies: 8
    Last Post: 02-27-2002, 09:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21