0

It was working before I tried to load my image.

This is the error I get:

Error 1 error LNK2005: "struct SDL_Window * m_pWindow" (?m_pWindow@@3PAUSDL_Window@@A) already defined in Game.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj

Error 2 error LNK2005: "struct SDL_Renderer * m_pRenderer" (?m_pRenderer@@3PAUSDL_Renderer@@A) already defined in Game.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj

Error 3 error LNK2005: "struct SDL_Texture * m_pTexture" (?m_pTexture@@3PAUSDL_Texture@@A) already defined in Game.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj

Error 4 error LNK2005: "struct SDL_Rect m_sourceWREK"
(?m_sourceWREK@@3USDL_Rect@@A) already defined in Game.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj

Error 5 error LNK2005: "struct SDL_Rect m_destWREK"
(?m_destWREK@@3USDL_Rect@@A) already defined in Game.obj C:\Users\Joseph\Desktop\DuckGotti\DuckGotti\maine.obj

Error 7 error LNK1169: one or more multiply defined symbols found
C:\Users\Joseph\Desktop\DuckGotti\Debug\DuckGotti.exe 1

Here is my code (main.cpp):

#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
#include <iostream>
#include "Game.h"






/**my first game obj**/
Game* g_game = 0;


int main(int argc, char* argv[])
{
g_game = new Game();
g_game->init("Duck Gotti", 100, 100, 640, 480,  SDL_WINDOW_FULLSCREEN );

while (g_game->running())
{
    g_game->handelEvents();
    g_game->update();
    g_game->render();
}
g_game->clean();
return 0;
}

Game.h

#ifndef __Game__
#define __Game__
#include <SDL.h>
#include <SDL_image.h>
#include <stdio.h>
#include <string>
#include <iostream>

SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;

SDL_Texture* m_pTexture;
SDL_Rect m_sourceWREK;
SDL_Rect m_destWREK;

class Game
{
public:
Game(){}
~Game(){}

/**setting the run var to true**/
bool init(const char* title, int xpos, int ypos, int width, int height
,bool fullscreen);

void render();
void update();
void handelEvents();
void clean();
/** a func to access the privet var **/
bool running() { return m_bRunning; }

private:
SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;
    bool m_bRunning;

};

#endif /**defined (__Game__) **/

Game.cpp:

#include "game.h"


bool Game::init(const char* title, int xpos, int ypos, int width, 
int height, bool fullscreen )
{
int flags = 0;
if (fullscreen)
{
    flags = SDL_WINDOW_FULLSCREEN;
}
/**attempt to initalize SDL**/
if (SDL_Init(SDL_INIT_EVERYTHING) == 0)
{
    std::cout << "SDL init sucess/n";
    /**init the winn**/
    m_pWindow = SDL_CreateWindow(title, xpos, ypos, width, height, flags);
    if (m_pWindow !=0)
    {
        std::cout << "window creation sucess/n";
        m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);

    if (m_pRenderer != 0)
    {
        std::cout << "renderer created/n";
        SDL_SetRenderDrawColor(m_pRenderer, 255, 255, 255, 255);
    }
    else
    {
        std::cout << "renderer broked/n";
        return false;
    }
}
else
{
    std::cout << "winn init nope/n";
    return false;
}
}
else
{
    std::cout << "SDL init broked/n";
    return false;
}
std::cout << "init sucess/n";
m_bRunning = true;
return true;

SDL_Surface* pTempSurf = SDL_LoadBMP("img3.bmp");
m_pTexture = SDL_CreateTextureFromSurface(m_pRenderer, pTempSurf);
SDL_FreeSurface(pTempSurf);
SDL_QueryTexture(m_pTexture, NULL, NULL, &m_sourceWREK.w, &m_destWREK.h);

m_destWREK.x = m_sourceWREK.x = 0;
m_destWREK.y = m_sourceWREK.y = 0;
m_destWREK.w = m_sourceWREK.w;
m_destWREK.h = m_sourceWREK.h;


}
void Game::render()
{
SDL_RenderClear(m_pRenderer);
SDL_RenderCopy(m_pRenderer, m_pTexture, &m_sourceWREK, &m_destWREK);
SDL_RenderPresent(m_pRenderer);

}
void Game::update()
{


}
void Game::handelEvents()
{
SDL_Event evil;
if (SDL_PollEvent(&evil))
{
    switch (evil.type)
    {
    case SDL_QUIT:
        m_bRunning = false;
        break;
    default:
        break;
    }
}
}
void Game::clean()
{
SDL_DestroyWindow(m_pWindow);
SDL_DestroyRenderer(m_pRenderer);
SDL_Quit();

}
Community
  • 1
  • 1
Joey Hill
  • 13
  • 3

1 Answers1

2

I believe you are reading this book SDL Game Development. Be aware, the book has a bunch of errors.

remove these lines

SDL_Window* m_pWindow;
SDL_Renderer* m_pRenderer;

SDL_Texture* m_pTexture;
SDL_Rect m_sourceWREK;
SDL_Rect m_destWREK;

They are already defined inside game class.

I was reading this book, luckily I was organized and I kept the files. It seems you are in the first chapter

main.cpp

#include "game.h"

Game* g_game = 0;


int main(int argc, char* args[])
{
    g_game = new Game();
    g_game->init("Chapter 1: Setting up SDL", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);


    while( g_game->running() ){

        g_game->handleEvents();
        g_game->update();
        g_game->render();
        SDL_Delay(10);
    }

    g_game->clean();

    return 0;
}

game.h

#ifndef _GAME_H
#define _GAME_H

#include "SDL.h"

class Game
{
public:
    Game() {}
    ~Game() {}
    bool init(const char* title, int xpos, int ypos, int height, int width, int flags);
    void render();
    void update();
    void handleEvents();
    void clean();

    bool running() { return m_bRunning; }

private:
    SDL_Window*   m_pWindow;
    SDL_Renderer* m_pRenderer;

    bool m_bRunning;

};



#endif /* defined(_GAME_H) */

game.cpp

#include "game.h"
#include <iostream>


bool Game::init(const char* title, int xpos, int ypos, int height, int width, int flags)
{
    if ( SDL_Init(SDL_INIT_EVERYTHING) == 0 ){
        std::cout << "SDL init success\n";
        m_pWindow = SDL_CreateWindow(title, xpos, ypos, height, width, flags);

        if ( m_pWindow != 0 ){
            std::cout << "window creation success  \n";
            m_pRenderer = SDL_CreateRenderer(m_pWindow, -1, 0);

            if ( m_pRenderer != 0 ){
                std::cout << "renderer creation success\n";
                SDL_SetRenderDrawColor(m_pRenderer, 0, 0, 0, 255);
            }else{
                std::cout << "renderer init fail\n";
                return false;
            }
        }else{
            std::cout << "window init fail\n";
            return false;
        }
    }else{
        std::cout << "SDL init fail\n";
        return false;
    }

    std::cout << "init success\n";
    m_bRunning = true;
    //@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@



    return true;
}

void Game::render()
{
    SDL_SetRenderDrawColor(m_pRenderer, 0, 0, 0, 255);
    SDL_RenderClear(m_pRenderer);
    SDL_RenderPresent(m_pRenderer);
}

void Game::clean()
{
    std::cout << "cleaning game\n";
    SDL_DestroyWindow(m_pWindow);
    SDL_DestroyRenderer(m_pRenderer);
    SDL_Quit();
}


void Game::handleEvents()
{
    SDL_Event event;

    if ( SDL_PollEvent(&event) ){

        switch( event.type ){
            case SDL_QUIT:
                m_bRunning = false;
            break;

            default:
            break;
        }

    }
}

void Game::update()
{

}

These are some notes I've written

enter image description here

enter image description here

enter image description here

After chapter 2, you need to organize your classes because you will get lost easily with the inheritance. For example, the below picture, you will see the way classes are connected in chapter 3

enter image description here

For chapter 4,

enter image description here

Hope this helps.

CroCo
  • 5,531
  • 9
  • 56
  • 88
  • kk got it ! more question is The Black Art of Multiplatform Game Programming by Jazon Yamamoto any better? – Joey Hill May 24 '15 at 19:20
  • @JoeyHill, sorry I haven't read this book but from the sample, it seems the old version of sdl is used which means you need to rewrite the code to fit the SDL 2. – CroCo May 25 '15 at 22:39
  • Also note that you have to declare global variables in header files as extern and then define them in of the cpp files. Here is a brief explanation: https://stackoverflow.com/a/19929727/8715 and here is a much much longer one: https://stackoverflow.com/a/1433387/8715 – AntonioCS Apr 12 '21 at 17:45