Having an issue with headers

I'm a newbie in c + +, but I have experience doing simple programs, this is the first time I'm using headers because the program is going to be much bigger than it is right now ... I am following a guide which is this: http://www.sdltutorials.com/sdl-tutorial-basics, I go for the tutorial number 2 this guide is discontinued but im working on it by using SDL 2.0, as I'm using it for the applied logic, and so I sdl learning the basics of a more orderly application of the same.

my problem is this

error: 'gRenderer' was not declared in this scope|


i declared gRenderer in CApp.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#ifndef _CAPP_H_
#define _CAPP_H_

#include <SDL.h>

class CApp {

    private:
        bool            Running;


        SDL_Window*     window;
        SDL_Texture*    Surf_Display;

        int             SCREEN_WIDTH;
        int             SCREEN_HEIGHT;

    public:


        SDL_Renderer*   gRenderer;
        SDL_Texture*    gTexture;
        CApp();
        int OnExecute();



    public:

        bool OnInit();

        void OnEvent(SDL_Event* Event);

        void OnLoop();

        void OnRender();

        void OnCleanup();

};

#endif


this is CApp.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include "CApp.h"
#include "CApp_Texture.h"
#include <stdio.h>



CApp::CApp() {
    Running = true;
    window = NULL;
    gRenderer=NULL;
    SCREEN_WIDTH=640;
    SCREEN_HEIGHT=480;
}

int CApp::OnExecute() {

    if(OnInit() == false) {
        return -1;
    }

    SDL_Event Event;

    while(Running) {
        while(SDL_PollEvent(&Event)) {
            OnEvent(&Event);
        }

        OnLoop();
        OnRender();
    }

    OnCleanup();

    return 0;

}

int main(int argc, char* argv[]) {
    CApp theApp;

    return theApp.OnExecute();
}



this is the part where is the prob

CApp_Texture.h

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#ifndef _CAPP_TEXTURE_H_
#define _CAPP_TEXTURE_H_



#include <SDL.h>


class Texture {

    public:
        Texture();

    public:
        static SDL_Texture* loadTexture( char* File);
};

#endif 


and the .cpp

CApp_Texture.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include "CApp_Texture.h"
#include "CApp.h"
#include <stdio.h>


Texture::Texture() {
}

SDL_Texture* Texture ::loadTexture(char * File){
    //The final texture
    SDL_Texture* newTexture = NULL;

    //Load image at specified path
    SDL_Surface* loadedSurface = SDL_LoadBMP(File);
    if( loadedSurface == NULL )
    {
        printf( "Unable to load image!");
    }
    else
    {

        //Create texture from surface pixels

        newTexture = SDL_CreateTextureFromSurface(gRenderer, loadedSurface );
        if( newTexture == NULL )
        {
            printf( "Unable to create texture ");
        }
        //Get rid of old loaded surface
        SDL_FreeSurface( loadedSurface );
    }

    return newTexture;
}


What is the problem if im declaring CApp.h in CApp_Texture.cpp?.
Texture has no member gRenderer.

And what it means, is that it has absolutely no knowledge about what gRenderer might be. If you want to use it, you probably should do something like this:
1
2
3
4
5
6
7
8
//CApp_Texture.cpp

SDL_Texture* Texture::loadTexture(char* file, Capp* app)
{
//code...
newTexture = SDL_CreateTextureFromSurface(app->gRenderer, loadedSurface);
//code...
}


And after fixing this, please, change the way of accessing your data - everything should be hidden behind an interface. One of the reasons is that, if anybody(including you) decides to use this program, and later on, after improving, you want to change the way App works, you may also want to change the way it uses SDL_Renderer(throw it away, get it to other class, and so on). It will force you to make many changes in many places in code, and it's just a gate for a horde of bugs. If you hide it behind some function(e.g. GetRenderer() ), then the only place you have to change code would be the mentioned function. It's really useful sometimes, but you can't appreciate it until you run into described situation :)

Anyway, good luck.
Cheers!
Last edited on
Texture has no member gRenderer.

And what it means, is that it has absolutely no knowledge about what gRenderer might be. If you want to use it, you probably should do something like this:
1
2
3
4
5
6
7
8
//CApp_Texture.cpp

SDL_Texture* Texture::loadTexture(char* file, Capp* app)
{
//code...
newTexture = SDL_CreateTextureFromSurface(app->gRenderer, loadedSurface);
//code...
}


And after fixing this, please, change the way of accessing your data - everything should be hidden behind an interface. One of the reasons is that, if anybody(including you) decides to use this program, and later on, after improving, you want to change the way App works, you may also want to change the way it uses SDL_Renderer(throw it away, get it to other class, and so on). It will force you to make many changes in many places in code, and it's just a gate for a horde of bugs. If you hide it behind some function(e.g. GetRenderer() ), then the only place you have to change code would be the mentioned function. It's really useful sometimes, but you can't appreciate it until you run into described situation :)

Anyway, good luck.
Cheers!


i think i got your idea, but thats why i made the CApp_Texture.cpp/h, i think this will be the only function that loads textures to work with, and if i'm in the situation that i got a way to improve it, i'll only code in CApp_Texture.cpp, is that what do you mean?
Topic archived. No new replies allowed.