C - SDL (Simple Directmedia Layer) - Creating a rectangle and moving it

We are going to see, with this tutorial, how to create a shape on a window and how to move it with keyboard arrows.

We need, for this example, three files that we are going to create to dispatch the code:

  • main.c
  • manager_sdl.c
  • h.h

Of course, you can add a Makefile to easily compile and link files.
But to link the .o, you need to add the -lSDL command.

1. main.c

 

#include <string.h>

#include <errno.h>

#include <stdlib.h>

#include <SDL/SDL.h>

#include "h.h"


int init(t_sdl *sdl)

{

if (SDL_Init(SDL_INIT_EVERYTHING) == -1)

{

fprintf(stderr, "Error: %s\n", strerror(errno));

return (1);

}

if ((sdl->screen = SDL_SetVideoMode(800, 600, 32, SDL_HWSURFACE)) == NULL)

{

fprintf(stderr, "Error: %s", SDL_GetError());

return (1);

}

SDL_WM_SetCaption("Hello World! :D", NULL);

if (SDL_FillRect(sdl->screen, NULL, SDL_MapRGB(sdl->screen->format, 255, 32, 45)) == -1)

{

fprintf(stderr, "Error: %s\n", SDL_GetError());

return (1);

}

if (SDL_Flip(sdl->screen) == -1)

{

fprintf(stderr, "Error: %s\n", SDL_GetError());

return (1);

}


sdl->position->x = 0;

sdl->position->y = 0;

if (manager_event(sdl))

{

fprintf(stderr, "Error: %s\n", SDL_GetError());

return (1);

}

SDL_FreeSurface(sdl->rectangle);

return (0);

}


int main ()

{

t_sdl sdl;


sdl.screen = NULL;

if(init(&sdl))

return (1);

return (0);

}
 

2. manager_sdl.c

 

#include <stdio.h>

#include <string.h>

#include <SDL/SDL.h>

#include "h.h"


int manager_event(t_sdl *sdl)

{

int gogogo;

SDL_Event  event;


gogogo = 1;

while (gogogo)

{

 SDL_WaitEvent(&event);

 if (event.type == SDL_QUIT)

 gogogo = 0;

 else if (event.type == SDL_KEYDOWN)

 {

 if (event.key.keysym.sym == SDLK_ESCAPE)

 {

 printf("ESC\n");

 gogogo = 0;

 }

 if (event.key.keysym.sym == SDLK_RIGHT)

 {

 printf("RIGHT\n");

 sdl->position->x += 10;

 }

 if (event.key.keysym.sym == SDLK_LEFT)

 {

 printf("LEFT\n");

 sdl->position->x -= 10;

 }

 if (event.key.keysym.sym == SDLK_UP)

 {

 printf("UP\n");

 sdl->position->y -= 10;

 }

 if (event.key.keysym.sym == SDLK_DOWN)

 {

 printf("DOWN\n");

 sdl->position->y += 10;

 }

       }

 creation_rect(sdl);

}

return (0);

}


int creation_rect(t_sdl *sdl)

{

sdl->rectangle = NULL;

if (SDL_FillRect(sdl->screen, NULL, SDL_MapRGB(sdl->screen->format, 255, 32, 45)) == -1)

{

fprintf(stderr, "Error: %s\n", SDL_GetError());

return (1);

}

if ((sdl->rectangle = SDL_CreateRGBSurface(SDL_HWSURFACE, 220, 180, 32, 0, 0, 0, 0)) == NULL)

{

fprintf(stderr, "Error: %s\n", SDL_GetError());

return (1);

}

if (SDL_FillRect(sdl->rectangle, NULL, SDL_MapRGB(sdl->screen->format, 255, 255, 45)) == -1)

{

fprintf(stderr, "Error: %s\n", SDL_GetError());

return (1);

}

if (SDL_BlitSurface(sdl->rectangle, NULL, sdl->screen, sdl->position) == -1)

{

fprintf(stderr, "Error: %s\n", SDL_GetError());

return (1);

}

if (SDL_Flip(sdl->screen) == -1)

{

fprintf(stderr, "Error: %s\n", SDL_GetError());

return (1);

}

return (0);

}
 

3. h.h

 

 

#ifndef H_H_

#define H_H_


/**

 * STRUCTURE

 */

typedef struct s_sdl

{

SDL_Surface *screen;

SDL_Surface *rectangle;

SDL_Rect *position;


} t_sdl;


/**

 * PROTOTYPE

 */

/* main.c */

int init(t_sdl *sdl);

/* manager_sdl.c */

int manager_event(t_sdl *sdl);

int creation_rect(t_sdl *sdl);


#endif /* H_H_ */

4. Compiling, linking, running, playing

$ gcc main.c manager_sdl.c -c ; gcc main.o manager_sdl.o -lSDL -o go ; ./go

Well done, you made it. wink

Add new comment

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Refresh Type the characters you see in this picture. Type the characters you see in the picture; if you can't read them, submit the form and a new image will be generated. Not case sensitive.  Switch to audio verification.