Sign in to follow this  
Qweasdzxcaa Qweasdzxcaal

Passing Setup class to get screen height

Recommended Posts

There seems to be something wrong with the way i pass the variable SCREEN_HEIGHT:

from this class:

class First {
    public:
        First();
        ~First();
         SDL_Renderer* renderer = NULL;
         SDL_Window* gWindow = NULL;

        int SCREEN_WIDTH;
        int SCREEN_HEIGHT;
};

to this:

class Second {
    public:
        Second(First f);
        int y;
};

and this is the implementation for the classes :

#include "First.h"
First :: First()
: SCREEN_WIDTH(640), SCREEN_HEIGHT(SCREEN_WIDTH/16*9)
{
    SDL_Init( SDL_INIT_VIDEO );

    SDL_SetHint( SDL_HINT_RENDER_SCALE_QUALITY, "1" );

    gWindow = SDL_CreateWindow( "Testing", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );

    renderer = SDL_CreateRenderer( gWindow, -1, SDL_RENDERER_ACCELERATED );

    SDL_SetRenderDrawColor( renderer, 0xFF, 0xFF, 0xFF, 0xFF );

    IMG_Init( IMG_INIT_PNG );
}

First :: ~First()
{
SDL_DestroyRenderer( renderer );
SDL_DestroyWindow( gWindow );
gWindow = NULL;
renderer = NULL;

IMG_Quit();
SDL_Quit();
}

#include "Second.h"

Second :: Second(First f) {
    y = f.SCREEN_HEIGHT / 2;
}

 

what i dont understand is why do i get from SDL_GetError() invalid texture ?

this is the main function:
#include "First.h"
#include "Second.h"
#include <iostream>
int main( int argc, char* args[] )
{
    First first;
    Second second(first);
    bool quit = false;
    SDL_Event e;

    while( !quit )
    {
        while( SDL_PollEvent( &e ) != 0 )
            {
                if( e.type == SDL_QUIT )
                {
                    quit = true;
                }
            }
            SDL_SetRenderDrawColor( first.renderer, 0x00, 0x00, 0x00, 0xFF );
            SDL_RenderClear( first.renderer );
            std::cout << SDL_GetError() << std::endl;
            SDL_RenderPresent( first.renderer );
    }
return 0;
}
 

 

Share this post


Link to post
Share on other sites
Lactose    11471

Why do you think there's a problem with second.y? You create an instance of Second, but that instance is never used anywhere.

To find out where your error about invalid textures comes from (you should really copy error messages verbatim, not paraphrase them), you should check every SDL call you make. Currently, you seem to almost 10 SDL calls before your first SDL_GetError() call.

Basically, check the return values and see the first point it tells you something's wrong.

 

Also, this is a small enough program that I would definitely recommend you learning how to use a debugger (setting breakpoints, looking at variables and seeing the flow through your code, etc), if you don't already know how.

Share this post


Link to post
Share on other sites

I did check every variable and function.
the problem comes from

y = f.SCREEN_HEIGHT / 2;

everything else works as intended.
if i put the code in main it will work with no problem, but when i put it in a different file there is the error invalid texture from SDL_GetError();
maybe im passing the first class variable wrong ?

Edited by mudvayne

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this