Jump to content

SDL2, Gravidade?


edub13
 Share

Recommended Posts

Para ser sincero muito poucas:

Aqui vai o meu código (não está em C++ mas vou colocar mais tarde)

main.h

#include "SDL2/SDL.h"
#include "SDL2/SDL_image.h"
#include <iostream>
void Init(const char* title, int x, int y, int w, int h, Uint32 flags);
void Loop();
void Update();
void LoadResources();
void Draw();
bool isColliding(SDL_Rect* a, SDL_Rect* b);
bool WColliding(SDL_Rect* a);
void Exit();

SDL_Window* window;
SDL_Renderer* renderer;
SDL_Texture* background;
SDL_Texture* platform;
SDL_Texture* player;
int player_velX = 0;
int player_velY = 0;
SDL_Texture* ground;
SDL_Rect dstGroundRect = {0, (480 - dstGroundRect.h), 60, 60};
SDL_Rect platformRect = {0, 300, 150, 35};
SDL_Rect playerRect = {0, ((480 - 48) - 60), 20, 48};
SDL_Event event;
bool quit = false;
// Carregar imagens.
SDL_Texture* LoadIMG(const char* path, SDL_Renderer* r)
{
SDL_Texture* texture = nullptr;
SDL_Surface* surface = IMG_Load(path);

if(!surface)

	throw SDL_GetError();
else
	texture = SDL_CreateTextureFromSurface(r, surface);

	if(!texture)

		throw SDL_GetError();

SDL_FreeSurface(surface);

return texture;
}
// Inicializar SDL2.
void Init(const char* title, int x, int y, int w, int h, Uint32 flags){

 if(SDL_Init(SDL_INIT_VIDEO) != 0)
throw SDL_GetError();
 else{
window = SDL_CreateWindow(title, x, y, w, h, flags);
if(!window)
  throw SDL_GetError();
else{
  renderer = SDL_CreateRenderer(window, -1, 0);
  if(!renderer)
throw SDL_GetError();
}
 }
}
// Game loop
void Loop(){
 while(!quit){
while(SDL_PollEvent(&event)){
  switch(event.type){
case SDL_QUIT:
  quit = true;
  break;
case SDL_KEYDOWN:
  if(event.key.keysym.sym == SDLK_LEFT)
 player_velX = -3;
  if(event.key.keysym.sym == SDLK_RIGHT)
 player_velX =  3;


  break;
case SDL_KEYUP:
  if(event.key.keysym.sym == SDLK_LEFT)
 player_velX = 0;
  if(event.key.keysym.sym == SDLK_RIGHT)
 player_velX = 0;

  break;
  }
}

if(isColliding(&playerRect, &dstGroundRect) == true){
  playerRect.y = (480-60);
}

if(WColliding(&playerRect) == true){
  player_velX = 0;
}

playerRect.x += player_velX;
playerRect.y += player_velY;

Update();
 }
}
void Update(){
 SDL_RenderClear(renderer);
 Draw();
 SDL_RenderPresent(renderer);
}
void LoadResources(){

 background = LoadIMG("background.png", renderer);
 platform = LoadIMG("platform.png", renderer);
 player = LoadIMG("player.png", renderer);
 ground = LoadIMG("ground.png", renderer);
}
void Draw(){

 LoadResources();

 // Desenhar Fundo
 SDL_RenderCopy(renderer, background, NULL, NULL);

 // Desenhar "chão"
 SDL_RenderCopy(renderer, ground, NULL, &dstGroundRect);
 dstGroundRect.x = 0;
 SDL_RenderCopy(renderer, ground, NULL, &dstGroundRect);
 dstGroundRect.x = 0 + 60;
 SDL_RenderCopy(renderer, ground, NULL, &dstGroundRect);
 dstGroundRect.x = 0 + 120;
 SDL_RenderCopy(renderer, ground, NULL, &dstGroundRect);
 dstGroundRect.x = 0 + 180;
 SDL_RenderCopy(renderer, ground, NULL, &dstGroundRect);
 dstGroundRect.x = 0 + 240;
 SDL_RenderCopy(renderer, ground, NULL, &dstGroundRect);
 dstGroundRect.x = 0 + 300;
 SDL_RenderCopy(renderer, ground, NULL, &dstGroundRect);
 dstGroundRect.x = 0 + 360;
 SDL_RenderCopy(renderer, ground, NULL, &dstGroundRect);
 dstGroundRect.x = 0 + 420;
 SDL_RenderCopy(renderer, ground, NULL, &dstGroundRect);
 dstGroundRect.x = 0 + 480;
 SDL_RenderCopy(renderer, ground, NULL, &dstGroundRect);
 dstGroundRect.x = 0 + 540;
 SDL_RenderCopy(renderer, ground, NULL, &dstGroundRect);
 dstGroundRect.x = 0 + 600;

 //Desenhar plataformas
 SDL_RenderCopy(renderer, platform, NULL, &platformRect);
 platformRect.x = 0;
 SDL_RenderCopy(renderer, platform, NULL, &platformRect);
 platformRect.x = 640 - 150;

 // Desenhar jogador
 SDL_RenderCopy(renderer, player, NULL, &playerRect);

}
bool isColliding(SDL_Rect* a, SDL_Rect* b)
{
if (a->x < b->x + b->w && a->x + a->w > b->x && a->y < b->y + b->h && a->h + a->y > b->y){
	return true;
}
return false;
}
bool WColliding(SDL_Rect* a)
{
if(a->x < 0){
	a->x = a->x + 1;
	return true;
}

if(a->y < 0){
	a->y = a->y + 1;
	return true;
}

if((a->x + a->w) > 640){
	a->x = 640 - a->w;
	return true;
}

if((a->y + a->h) > 480){
	a->y = 480 - a->h;
	return true;
}

return false;
}
void Exit(){

 SDL_DestroyTexture(platform);
 SDL_DestroyTexture(ground);
 SDL_DestroyTexture(background);

 SDL_DestroyRenderer(renderer);
 SDL_DestroyWindow(window);

 SDL_Quit();
}

main.cpp:

#include "main.h"
int main(){
 Init("Title", 0, 0, 640, 480, 0);
 Loop();
 Exit();
 return 0;
}
Edited by edub13

Learning:

C++
Link to comment
Share on other sites

?????


void Update(){
 SDL_RenderClear(renderer);
 Draw();
 SDL_RenderPresent(renderer);
}

void LoadResources(){
 background = LoadIMG("background.png", renderer);
 platform = LoadIMG("platform.png", renderer);
 player = LoadIMG("player.png", renderer);
 ground = LoadIMG("ground.png", renderer);
}

void Draw(){
 LoadResources(); // <--- em todos os frames estás a ler os ficheiros de imagem ??
                  //      não só é um desperdício como como é um total "memory leak" de tamanho descomunal !!!
IRC : sim, é algo que ainda existe >> #p@p
Link to comment
Share on other sites

Realmente, agora percebo porque estava tão lento, já mudei isso:

void Init(const char* title, int x, int y, int w, int h, Uint32 flags){

 if(SDL_Init(SDL_INIT_VIDEO) != 0)
throw SDL_GetError();
 else{
window = SDL_CreateWindow(title, x, y, w, h, flags);
if(!window)
  throw SDL_GetError();
else{
  renderer = SDL_CreateRenderer(window, -1, 0);
  if(!renderer)
       throw SDL_GetError();
}
 }

 LoadResources(); // Aqui está

}
Edited by edub13

Learning:

C++
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.