Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "SDL/SDL.h"
- #include <stdlib.h>
- #include <stdio.h>
- //This is actually C code, so I'm setting up a bool
- //type, like the one in C++.
- #define true 1
- #define false 0
- typedef int bool;
- //Sprite data structure.
- typedef struct sprite_t {
- int x, y; //Horizontal and vertical position, respectively. I assume these refer to the upper left corner of the sprite.
- int w, h; //Width and height of sprite, respectively.
- SDL_Surface *image;
- }sprite_t;
- //One of the few good reasons to use a global variable:
- //the screen surface
- SDL_Surface *screen;
- //set sprite width and height from image.
- void setup_sprite(sprite_t *sprite);
- //collision test function. Returns true if sprites are touching.
- bool collision(sprite_t sprite_1, sprite_t sprite_2);
- //render put the sprite on the screen
- void render(sprite_t sprite);
- int main(){
- //sprites
- sprite_t s1,s2;
- //load images
- s1.image = SDL_LoadBMP("sprite-1.bmp");
- s2.image = SDL_LoadBMP("sprite-2.bmp");
- //check if image loaded correctly:
- if(s1.image == NULL || s2.image == NULL){
- printf("Error: image load failed: %s\n", SDL_GetError());
- return -1;
- }
- //set sprite width from surface
- setup_sprite(&s1);
- setup_sprite(&s2);
- //Init SDL
- if (SDL_Init(SDL_INIT_VIDEO)){
- printf("Error: Unable to init SDL: %s\n", SDL_GetError());
- return -1;
- }
- //Create window
- screen = SDL_SetVideoMode(640, 480, 24, SDL_SWSURFACE);
- if(screen == NULL){
- printf("Error: Unable to create window: %s\n", SDL_GetError());
- return -1;
- }
- //set sprite positions.
- s1.x = 200;
- s1.y = 200;
- s2.x = 150;
- s2.y = 150;
- //main loop
- int done = 0;
- SDL_Event event;
- while(!done){
- //use the arrow keys to move s2 around.
- while(SDL_PollEvent(&event)){
- switch(event.type){
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym){
- case SDLK_UP: s2.y--; break;
- case SDLK_DOWN: s2.y++; break;
- case SDLK_RIGHT: s2.x++; break;
- case SDLK_LEFT: s2.x--; break;
- case SDLK_ESCAPE: done = 1; break;
- }
- //check if moving resulted in a collision.
- if(collision(s1,s2)){
- printf("collision!\n");
- }
- break;
- case SDL_QUIT:
- done = 1;
- break;
- }
- }
- //clear screen
- SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
- //render sprites
- render(s1);
- render(s2);
- SDL_Flip(screen);
- }
- return 0;
- }
- //collision test function. Returns true if sprites are touching.
- bool collision(sprite_t sprite_1, sprite_t sprite_2){
- //Let's examine the x axis first:
- //If the leftmost or rightmost point of the first sprite lies somewhere inside the second, continue.
- if( (sprite_1.x >= sprite_2.x && sprite_1.x <= (sprite_2.x + sprite_2.w)) ||
- ((sprite_1.x + sprite_1.w) >= sprite_2.x && (sprite_1.x + sprite_1.w) <= (sprite_2.x + sprite_2.w)) ){
- //Now we look at the y axis:
- if( (sprite_1.y >= sprite_2.y && sprite_1.y <= (sprite_2.y + sprite_2.h)) ||
- ((sprite_1.y + sprite_1.h) >= sprite_2.y && (sprite_1.y + sprite_1.h) <= (sprite_2.y + sprite_2.h)) ){
- //The sprites appear to overlap.
- return true;
- }
- }
- //This algorithm will not detect collisions if sprite_2 is smaller than sprite_1.
- //To solve this problem, we'll check again, using sprite_2 in place of sprite_1.
- if( (sprite_2.x >= sprite_1.x && sprite_2.x <= (sprite_1.x + sprite_1.w)) ||
- ((sprite_2.x + sprite_2.w) >= sprite_1.x && (sprite_2.x + sprite_2.w) <= (sprite_1.x + sprite_1.w)) ){
- //Now we look at the y axis:
- if( (sprite_2.y >= sprite_1.y && sprite_2.y <= (sprite_1.y + sprite_1.h)) ||
- ((sprite_2.y + sprite_2.h) >= sprite_1.y && (sprite_2.y + sprite_2.h) <= (sprite_1.y + sprite_1.h)) ){
- //The sprites appear to overlap.
- return true;
- }
- }
- //The sprites do not overlap:
- return false;
- }
- //set sprite width and height from image.
- void setup_sprite(sprite_t *sprite){
- //safety first:
- if(sprite == NULL)return;
- if(sprite->image == NULL)return;
- sprite->w = sprite->image->w;
- sprite->h = sprite->image->h;
- }
- //render put the sprite on the screen
- void render(sprite_t sprite){
- if(sprite.image == NULL)return;
- SDL_Rect drect; //used to store location image to blit
- drect.x = sprite.x;
- drect.y = sprite.y;
- SDL_BlitSurface(sprite.image, NULL, screen, &drect);
- }
Advertisement
Add Comment
Please, Sign In to add comment