Jump to content

SDL velocidade do "sprite"


edub13
 Share

Recommended Posts

na realidade ele deverá estar a se mover bem rápido. vê na consola o númeo de actualizações do quadrado. como disse o bsccara, altera a quantidade de pixels a serem incrementados à posição do quadrado

podes ver e brincar com este exemplo;

#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <memory.h>

#include <SDL/SDL.h>

#define WINDOW_WIDTH 480
#define WINDOW_HEIGHT 320

typedef struct {
time_t start_time;
int count;
int fps;
} FPS;

void fpsInit(FPS * fps) {
fps->start_time = time(NULL);
fps->count = 0;
fps->fps = 0;
}

void fpsFrame(FPS * fps, SDL_Surface * screen) {
time_t current_time = time(NULL);
char buffer[256];

fps->count++;
if (current_time != fps->start_time) {
	sprintf(buffer, "FPS : %d", fps->count);
	SDL_WM_SetCaption(buffer, NULL);

	fps->count = 0;
	fps->start_time = current_time;
}

SDL_Flip(screen);
}

#define SQUARE_INCREMENT 2

typedef struct {
SDL_Rect pos;
int color;

SDL_Rect next_pos;
int update_pos;
} Square;

void squareInit(Square * square, int x, int y, int h, int w, int color) {
square->pos.x = 10;
square->pos.y = 10;
square->pos.w = 20;
square->pos.h = 20;
square->color = color;

memcpy(&square->next_pos, &square->pos, sizeof(SDL_Rect));
square->update_pos = 0;
}

void squareMoveUp(Square * square, int increment) {
square->next_pos.y = square->pos.y - increment;
square->update_pos = 1;
}

void squareMoveDown(Square * square, int increment) {
square->next_pos.y = square->pos.y + increment;
square->update_pos = 1;
}

void squareMoveLeft(Square * square, int increment) {
square->next_pos.x = square->pos.x - increment;
square->update_pos = 1;
}

void squareMoveRight(Square * square, int increment) {
square->next_pos.x = square->pos.x + increment;
square->update_pos = 1;
}

void squareUpdate(Square * square) {
if (square->update_pos) {
	printf("SQUARE : %dx%d\n", square->pos.x, square->pos.y);
	memcpy(&square->pos, &square->next_pos, sizeof(SDL_Rect));
	square->update_pos = 0;
}
}

int main() {
SDL_Surface * screen = NULL;
SDL_Event event;
Square square;
FPS fps;
int run = 0;

if (SDL_Init(SDL_INIT_VIDEO) != 0) {
	printf("Unable to start SDL\n");
	exit(-1);
}
atexit(SDL_Quit);

SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY, SDL_DEFAULT_REPEAT_INTERVAL);

if ((screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 24, SDL_DOUBLEBUF | SDL_HWSURFACE)) == NULL) {
	printf("Unable to create window\n");
	exit(-1);
}

squareInit(&square, 10, 10, 20, 20, SDL_MapRGB(screen->format, 255, 0, 0));
fpsInit(&fps);

run = 1;
while (run) {
	while (SDL_PollEvent(&event)) {
		switch (event.type) {
			case SDL_QUIT:
				run = 0;
				break;
			case SDL_KEYDOWN:
				switch (event.key.keysym.sym) {
					case SDLK_UP:
						squareMoveUp(&square, SQUARE_INCREMENT);
						break;
					case SDLK_DOWN:
						squareMoveDown(&square, SQUARE_INCREMENT);
						break;
					case SDLK_LEFT:
						squareMoveLeft(&square, SQUARE_INCREMENT);
						break;
					case SDLK_RIGHT:
						squareMoveRight(&square, SQUARE_INCREMENT);
						break;
					default:
						break;
				}
				break;
			default:
				break;
		}
	}

	SDL_FillRect(screen, NULL, 0);

	squareUpdate(&square);
	SDL_FillRect(screen, &square.pos, square.color);

	fpsFrame(&fps, screen);
}

SDL_Quit();
return 0;
}
IRC : sim, é algo que ainda existe >> #p@p
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.