1

I'm Remaking Galaga to learn SDL2/SDL. I managed to render a sprite and display and create the windo and game loop. Problem is, The sprite takes up the whole screen. Is there way to fix this? Here's the code:

#include <iostream>
#include <SDL2/SDL.h>
#define SPRITE_SIZE    32

const int WIDTH = 448, HEIGHT = 576;

int main( int argc, char *argv[] )
{
    std::cout<<"Starting SDL...\n";
    SDL_Init( SDL_INIT_EVERYTHING);
    std::cout<<"Creating Window...\n";
    SDL_Window *window = SDL_CreateWindow( "GALAGA", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, WIDTH, HEIGHT, SDL_WINDOW_ALLOW_HIGHDPI);
    if (NULL == window)
    {
        std::cout<<"Failed to create window" << SDL_GetError() << std::endl;
        return 1;
    }


    std::cout<<"Adding Renderer...\n";
    SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, 0);
    SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);

    /* Load bitmap image */
    SDL_Surface *bmp = SDL_LoadBMP("./Ship.bmp");
    if (bmp == nullptr) {
        std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl;
        return 1;
    }

 /* Upload surface to render, and then, free the surface */
        SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, bmp);
    SDL_FreeSurface(bmp);
    if (texture == nullptr){
        std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
        return 1;
    }

    /* Draw the render on window */
    SDL_RenderClear(renderer); // Fill render with color
    SDL_RenderCopy(renderer, texture, NULL, NULL); // Copy the texture into render
    SDL_RenderPresent(renderer); // Show render on window

    SDL_Event windowEvent;
    while (true)
    {
        if (SDL_PollEvent( &windowEvent))
        {
            if (SDL_QUIT == windowEvent.type )
            {
                break;
            }
        }
    }

    SDL_DestroyWindow(window);
    SDL_Quit();

    return EXIT_SUCCESS;

}

The rendered sprite is Ship.bmp.

The sprite take up the whole window and I'm trying to fix it.

  • Gotta say filling the whole screen with the sprite is an easy way to get collision detection working. – user4581301 Jun 02 '23 at 00:54
  • That would be useful. I'd love to learn how, but I need to get this ship to right size first. Do you happen to have a solution? – EnderCal1012 Jun 02 '23 at 01:03
  • Not in SDL, I'm afraid. Doesn't come up in embedded systems all that often. But I suspect it'll turn out to be something to do with the texture being copied into the renderer without a bounding rectangle . – user4581301 Jun 02 '23 at 01:09
  • 1
    [Here's what the docs say about parameter 4](https://wiki.libsdl.org/SDL2/SDL_RenderCopy): *`dstrect` the destination `SDL_Rect` structure or **`NULL` for the entire rendering target**; the texture will be stretched to fill the given rectangle.* Emphasis mine. – user4581301 Jun 02 '23 at 01:12
  • That may work, but i'm still new to this. How would I write that? an example would help. – EnderCal1012 Jun 02 '23 at 01:14
  • I thought I had a good comment, but I don't know the source material well enough to try it out and should just shut the up at this point. Search Stackoverflow for *SDL Render Sprite* with your favorite search engine (SO's search is... not so good) and you should find someone doing almost exactly what you want. – user4581301 Jun 02 '23 at 01:24
  • 1
    I'd be careful with that duplicate: both the question and the first answer date from 2010 before SDL2 even existed. The second answer is relevent but doesn't provide much more than the SDL2 wiki page for [`SDL_RenderCopy`](https://wiki.libsdl.org/SDL2/SDL_RenderCopy). You might want to use [`SDL_QueryTexture`](https://wiki.libsdl.org/SDL2/SDL_QueryTexture) to get the size of the sprite, and then put that size along with a position into a `SDL_Rect` that you then pass to `SDL_RenderCopy` as 4th argument. – Nelfeal Jun 02 '23 at 10:09

0 Answers0