lundi 29 juin 2015

SDL textures no longer rendering after restructuring code


I have been following the tutorials found here and I decided to deviate a bit before moving on.

I made a basic texture wrapper with which rendering textures worked fine. I then made a GameObject Class that stores a texture, a SDL_rect and, a dimension/location for the SDL_rect. I also tried to implement the standard update/draw loop with function pointers. So after setting all of this up, the textures that previously rendered perfectly fine (which I am testing with again) no longer work under this extra wrapper

From my main.cpp:

//the Texture that we will be applying an image on
Texture TextureOne("PNGTest.png");
Texture TextureTwo("Test2.png");
Texture TextureThree("Test3.png");

GameObject Background(TextureTwo);
GameObject Viewport(TextureOne);
GameObject TextBoxBackground(TextureThree);

first I declare my objects:

Background.Dimensions.x = SCREEN_WIDITH / 2;
Background.Dimensions.y = SCREEN_HEIGHT / 2;
Background.Location.x = SCREEN_WIDITH / 2;

Viewport.Dimensions.x = SCREEN_WIDITH / 2;
Viewport.Dimensions.y = SCREEN_HEIGHT / 2;

TextBoxBackground.Dimensions.x = SCREEN_WIDITH;
TextBoxBackground.Dimensions.y = SCREEN_HEIGHT / 2;
TextBoxBackground.Location.y = SCREEN_HEIGHT / 2;

//clear screen
SDL_RenderClear(Renderer);

//for all objects in the object list
for(unsigned i = 0; i < ObjectList.size(); i++)
{
    //update the object
    ObjectList[i]->Update(*ObjectList[i]);
    //then draw the updated object
    ObjectList[i]->Draw(*ObjectList[i]);
}

//upadte screen
SDL_RenderPresent(Renderer);

Then I set up the various dimensions and locations and finally call the objects update and draw

Here is the game object constructor being used:

GameObject::GameObject(Texture TargetTexture)
{
    Dimensions.x = 1;
    Dimensions.y = 1;
    ObjectStorage.x = Location.x;
    ObjectStorage.y = Location.y;
    ObjectStorage.w = Dimensions.x;
    ObjectStorage.h = Dimensions.y;

    Update = DefaultUpdate;
    Draw = DefaultDraw;

    ObjectTexture = TargetTexture;

    ObjectList.push_back(this);
}

here is the update function:

float DefaultUpdate(GameObject& Self)
{
    float CurrentTime = SDL_GetTicks();

    Self.ObjectStorage.w = Self.Dimensions.x;
    Self.ObjectStorage.h = Self.Dimensions.y;
    Self.ObjectStorage.x = Self.Location.x;
    Self.ObjectStorage.y = Self.Location.y;

    if(CurrentTime > LastTime)
    {
        DeltaTime = (CurrentTime - LastTime) / 1000;
        LastTime = CurrentTime;
    }

    return DeltaTime;
}

and lastly the draw function:

void DefaultDraw(GameObject& Self)
{
    SDL_RenderSetViewport(Renderer, &Self.ObjectStorage);

    SDL_RenderCopy(Renderer, Self.ObjectTexture.GetCurrentTexture(),
                   NULL, NULL);
}

I have a sneaking suspicion that the issue is in the draw function, in that the SDL_Viewport/SDL_RenderCopy lose meaning after going out of scope, or that I should be updating the renderer with in the draw function, but I cannot seem to get those to work, are there any suggestions or should I just give up on this architecture entirely?


Aucun commentaire:

Enregistrer un commentaire