kannoli
kannoli

Reputation: 378

SDL, How can I make the background picture render?

I had a render problem, but I fixed that. The problem is, is that the picture won't actually show up. I know the picture gets loaded because I have an error handler if it isn't loaded. I know the code is messy, but I just need the picture to show up.

#include <SDL/SDL.h>
#include <stdio.h>
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

const int fps = 60;

SDL_Window *window;


int main(int argc, char **argv)
{
    SDL_Init(SDL_INIT_EVERYTHING);
    window = SDL_CreateWindow("", 300, 100, 1024, 800, SDL_WINDOW_OPENGL);

    if (window == NULL)
    {
        cout << ("could not create window: %s/n", SDL_GetError());
        return 1;
    }
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
    int x = 0, y = 0;
    SDL_Surface  *screen;
    SDL_Surface *background = SDL_LoadBMP("hockeyrink.bmp");
    if(background == NULL)
    {
        SDL_ShowSimpleMessageBox(0, "Background init error",         SDL_GetError(), window);
    }

    if(renderer == NULL)
    {
        SDL_ShowSimpleMessageBox(0, "Renderer init error", SDL_GetError(), window);
    }
    SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer,background);
    if(texture == NULL)
    {
        SDL_ShowSimpleMessageBox(0, "Texture init error", SDL_GetError(), window);
    }

    const int speed = 5;
    SDL_Rect camera;
    camera.x = 0; //Don't worry about this camera, I need this after i get the background working.
    camera.y = 0;
    camera.w = 800;
    camera.h = 600;
    bool b[2] = {0,0};
    Uint32 start;

    bool running = true;
    while (running)
    {
        start = SDL_GetTicks();
        SDL_Event event;

        while(SDL_PollEvent(&event))
        {
            switch(event.type)
            {
            case SDL_QUIT:
                running = false;
                SDL_DestroyTexture(texture);
                SDL_FreeSurface(background);
                SDL_DestroyRenderer(renderer);
                SDL_DestroyWindow(window);
                SDL_Quit();
                break;
            case SDL_KEYDOWN:
                switch(event.key.keysym.sym)
                {
                case SDLK_UP:
                    b[0]=1;
                    break;
                case SDLK_LEFT:
                    b[1]=1;
                    break;
                }
                break;
            case SDL_KEYUP:
                switch(event.key.keysym.sym)
                {
                case SDLK_UP:
                    b[0]=0;
                    break;
                case SDLK_LEFT:
                    b[1]=0;
                    break;
                }
                break;
            }
        }

        if(b[0])
        {
            x+=speed;
            camera.y+=speed;
            if (camera.y > 3000-800)
            {
                camera.y=0;
            }

        }
        else if(b[1])
        {
            x-=speed;
            camera.y-=speed;

            if (camera.y <= 0)
            {
                camera.y = 2000-800;
            }


            SDL_RenderCopy(renderer, texture, NULL, NULL);
            SDL_RenderPresent(renderer);
        }
        if(1000/fps>SDL_GetTicks()-start)
        {
            SDL_Delay(1000/fps-(SDL_GetTicks() - start));
        }
    }

    return 0;
}

Upvotes: 0

Views: 9225

Answers (1)

Mahesh Bansod
Mahesh Bansod

Reputation: 2033

You have initialized b[1] to 0 and the texture is rendered only when b[1] is 1 as in your code.
You should either put the lines:

SDL_RenderCopy(renderer, texture, NULL, NULL);
 SDL_RenderPresent(renderer);

before the loop,
or press the left key of your keyboard(i.e. SDLK_LEFT)
to render and present the image on the screen.

It works in my system, so it should work in yours, too.

Upvotes: 1

Related Questions