SDL2 image

Started by
18 comments, last by Choo Wagga Choo Choo 1 month ago

actually I want to draw a paddle at the right side of the screen and the middle of the screen.

Advertisement

Seems SDL has ‘viewports’, here is a blog making a example:

https://dev.to/noah11012/using-sdl2-viewports-dgn

Here is a Pong game using SDL:

https://github.com/toivjon/sdl2-pong

A collection of links with SDL tutorials:

https://wiki.libsdl.org/SDL2/Tutorials

That's random stuff i have found in a minute using Google. There surely is much more. Spend some time to find resources you like and feel easy to follow.

Btw, i assume you would not need to use SDL viewport tooling functions at all. Alternatively you could also use standard OpenGL code to set up your projection. SDL likely just makes it easier, but ofc. you need to figure out how to use it. Ideally SDL also helps with rendering sprites without a need to use glBegin, glVertex, glEnd, etc. But it's main purpose is to initialize window and rendering, have a 60 fps display, get keyboard and mouse input, and also play back audio.

here is my updated code how do I draw a bmp on the right side of the screen instead of the upper left of the screen like it is doing now.

/*This source code copyrighted by Lazy Foo' Productions 2004-2024
and may not be redistributed without written permission.*/

//Using SDL and standard IO
#include <SDL.h>
#include <stdio.h>

//Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

//Starts up SDL and creates window
bool init();

//Loads media
bool loadMedia();

//Frees media and shuts down SDL
void close();

//The window we'll be rendering to
SDL_Window* gWindow = NULL;
	
//The surface contained by the window
SDL_Surface* gScreenSurface = NULL;

//The image we will load and show on the screen
SDL_Surface* gHelloWorld = NULL;

bool init()
{
	//Initialization flag
	bool success = true;

	//Initialize SDL
	if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
	{
		printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
		success = false;
	}
	else
	{
		//Create window
		gWindow = SDL_CreateWindow( "SDL Tutorial", 800, 600, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
		if( gWindow == NULL )
		{
			printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
			success = false;
		}
		else
		{
			//Get window surface
			gScreenSurface = SDL_GetWindowSurface( gWindow );
		}
	}

	return success;
}

bool loadMedia()
{
	//Loading success flag
	bool success = true;

	//Load splash image
	gHelloWorld = SDL_LoadBMP( "C:\\Users\\Owner\\source\\repos\\Project94\\Debug\\paddle.bmp" );
	if( gHelloWorld == NULL )
	{
		printf( "Unable to load image %s! SDL Error: %s\n", "C:\\Users\\Owner\source\\repos\\Project94\\Debug\\paddle.bmp", SDL_GetError() );
		success = false;
	}

	return success;
}

void close()
{
	//Deallocate surface
	SDL_FreeSurface( gHelloWorld );
	gHelloWorld = NULL;

	//Destroy window
	SDL_DestroyWindow( gWindow );
	gWindow = NULL;

	//Quit SDL subsystems
	SDL_Quit();
}

int main( int argc, char* args[] )
{
	//Start up SDL and create window
	if( !init() )
	{
		printf( "Failed to initialize!\n" );
	}
	else
	{
		//Load media
		if( !loadMedia() )
		{
			printf( "Failed to load media!\n" );
		}
		else
		{
			//Apply the image
			SDL_BlitSurface( gHelloWorld, 0, gScreenSurface, 0 );
			
			//Update the surface
			SDL_UpdateWindowSurface( gWindow );

            //Hack to get window to stay up
            SDL_Event e; bool quit = false; while( quit == false ){ while( SDL_PollEvent( &e ) ){ if( e.type == SDL_QUIT ) quit = true; } }
		}
	}

	//Free resources and close SDL
	close();

	return 0;
}

Use the last parameter to SDL_BlitSurface to set the destination position. (width and height are ignored)
https://www.libsdl.org/release/SDL-1.2.15/docs/html/sdlblitsurface.html

how do I adjust the last parameter?

I dont know how to use "SDL_Rect *dstrect"

I am using

typedef struct{

Sint16 x,y;

Uint16 w,h;

} SDL_Rect;

I am going to study about structs

good.

Also experiment with pass argument by pointer or reference.
In this particular case it could be as simple as
//apply the image
SDL_Rect my_experimental_rect = SDL_Rect(100, 0, 0, 0); // todo: move me outside the main loop
SDL_BlitSurface(gHelloWorld, 0, gScreenSurface, &my_experimental_rect);

something like that gets you going.
Straight up though…I've never touched SDL. Hated GLFW. FreeGlut was even worse.
If I were to do what you're after, I'd go with raylib, Pick your language from there.
I'm with Joe and would be thinking in 3D but this is cool as well.
But yeah, you might have to tweak the implementation, but that's the basic shoehorn.

One step at a time Phil…you'll get there.

This topic is closed to new replies.

Advertisement