Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length

 
Advanced search

1075927 Posts in 44152 Topics- by 36120 Members - Latest Member: Royalhandstudios

December 29, 2014, 03:57:37 PM
TIGSource ForumsDeveloperTechnical (Moderators: Glaiel-Gamer, ThemsAllTook)Problem with SDL
Pages: [1]
Print
Author Topic: Problem with SDL  (Read 454 times)
Carlos_PRO
Guest
« on: July 02, 2012, 10:29:37 AM »

Hello Guys, i'm started with sdl and i have a little problem. The problem is that when you move the sprite drawing she leaves the "sprite" through which it passes. I am using the following code.

main.h
Code:
#ifndef _MAIN_H_
#define _MAIN_H_

#include <SDL.h>
#include <stdio.h>
#include <stdlib.h>

#define SCREEN_WIDTH 640
#define SCREEN_HEIGHT 480
#define SCREEN_BPP 32

SDL_Surface *screen = NULL;
SDL_Surface *image = NULL;

void sdl_init(void);
void sdl_finish(void);
void draw_image(int x, int y, int width, int height, SDL_Surface *screenSurface, SDL_Surface *imageSurface);

#endif
main.c
Code:
#include "main.h"

/* Init */
void sdl_init(void)
{
if(SDL_Init(SDL_INIT_EVERYTHING))
{
fprintf(stderr, "Error in initialization the systems: %s\n", SDL_GetError());
exit(-1);
}

screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE);
if(!screen)
{
fprintf(stderr, "Error to create the window: %s\n", SDL_GetError());
exit(-1);
}

SDL_WM_SetCaption("001", NULL);

return;
}

/* Finalization */
void sdl_finish(void)
{
SDL_FreeSurface(image);

SDL_Quit();
return;
}

/* Desenha as sprites */
void draw_image(int x, int y, int width, int height, SDL_Surface *screenSurface, SDL_Surface *imageSurface)
{
SDL_Rect dest, src;

src.x = 0;
src.y = 0;
src.w = width;
src.h = height;

dest.x = x;
dest.y = y;
dest.w = width;
dest.h = height;

SDL_BlitSurface(imageSurface, &src, screenSurface, &dest);

return;
}

/* Main Function */
int main(int argc, char *argv[])
{
int gameQuit = 1;
int imgX = 0,
imgY = 0;
SDL_Event event;
int key[323] = {0};

sdl_init();

image = SDL_LoadBMP("sPlayer.bmp");
if(!image)
{
fprintf(stderr, "Error to load image: %s\n", SDL_GetError());
exit(-1);
}

while(gameQuit)
{
while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
gameQuit = 0;

if(event.type == SDL_KEYDOWN)
{
key[event.key.keysym.sym] = 1;
}
if(event.type == SDL_KEYUP)
{
key[event.key.keysym.sym] = 0;
}
}

/* Keys */
if(key[SDLK_DOWN] && ((imgY + image->h) < SCREEN_HEIGHT))
imgY += 1;
if(key[SDLK_UP] && (imgY > 0))
imgY -= 1;
if(key[SDLK_RIGHT] && ((imgX + image->w) < SCREEN_WIDTH))
imgX += 1;
if(key[SDLK_LEFT] && (imgX > 0))
imgX -= 1;
if(key[SDLK_ESCAPE])
gameQuit = 0;

/* Draw */
draw_image(imgX, imgY, image->w, image->h, screen, image);

/* Update */
SDL_Flip(screen);
}

sdl_finish();
return 0;
}
But if i add the following piece of code in down of SDL_Flip.
Code:
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));
And here I have another problem because the sdl will not repeat over the sprite, but that will not cause error on the screen will be redrawn simply making the sprite is below the anterior surface of the screen causing errors later, because of the amount of images that would be on the screen. Or am I wrong.

I was wondering how do I update the image and not out showing a lot of repeated images. And if sdl_fillrect do this and I am wrong as to hide the images under the surface.
« Last Edit: July 02, 2012, 11:04:43 AM by Carlos_PRO » Logged
rivon
Level 10
*****



View Profile
« Reply #1 on: July 02, 2012, 10:52:48 AM »

You have to clear the screen before drawing the next frame:
Code:
SDL_FillRect(screen, 0, SDL_MapRGB(screen->format, 0, 0, 0));
Logged
Carlos_PRO
Guest
« Reply #2 on: July 02, 2012, 11:06:10 AM »

But if you use it simply does not chew SDL_FillRect other sprites drawn below the frames. Or she update screen deleting everything I had previously.
Logged
ASnogarD
Level 1
*



View Profile
« Reply #3 on: July 02, 2012, 11:26:04 AM »

You need to clear the screen and then redraw your sprite every loop to animate it ( ok there are ways and means to avoid redrawing the entire screen but beyond a basic explanation ).

put the

Code:
SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0));

bfore your

Quote
draw_image(imgX, imgY, image->w, image->h, screen, image);

this should clear your screen and draw the sprite again.

I would try reading Lazy Foo's tutorials on SDL for some more ideas on using SDL.
http://lazyfoo.net/index.php

and see sdltutorials.com for more ideas.
http://www.sdltutorials.com/
Logged

Somethings are painfully obvious, others must be made obvious... painfully.
Carlos_PRO
Guest
« Reply #4 on: July 02, 2012, 11:37:49 AM »

Thanks for the answers now and my only doubt whether SDL_FillRect redraw everything again or redraws anrtiga above the surface.
Logged
waxx
Level 1
*


head to the right


View Profile Email
« Reply #5 on: July 02, 2012, 11:48:30 AM »

fillrect just fills your screen with a chosen color
it's used just to clear the screen lol

you redraw everything manually by putting the whole thing into some sort of a game loop
Logged

Carlos_PRO
Guest
« Reply #6 on: July 02, 2012, 12:03:13 PM »

Now I understood how can I say SDL_FillRect will reset my screen and I'll have to redesign my "objects" all over again.
Logged
Pages: [1]
Print
Jump to:  

Theme orange-lt created by panic