#include <GLES/gl.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
const int SIZE = 11;
const int SCREEN_WIDTH = SIZE * 60;
const int SCREEN_HEIGHT = SIZE * 30;
const int AREA = SCREEN_WIDTH * SCREEN_HEIGHT;
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_Surface *backgroundImage = NULL;
SDL_Event event;
SDL_Texture *background = NULL;
const int CIMA = 1;
const int BAIXO = 2;
const int ESQUERDA = 3;
const int DIREITA = 4;
unsigned direcao = 0;
struct food
{
SDL_Rect posLivre;
int x;
int y;
} food;
struct snake
{
/* head postion */
int x;
int y;
/* try this */
snake *boody;
/* catch this */
SDL_Rect segmento[AREA];
/* how long is snace */
int totalSegmento = 2;
/* ??? */
int dimensao;
} snake, head =
{
20, 0}, speed =
{
2, 2};
bool BodyCollision(int x, int y)
{
for (int i = 0; i < snake.totalSegmento; i++)
if ((snake.segmento[i].x == x) && (snake.segmento[i].y == y))
return true;
return false;
}
void go()
{
switch (direcao)
{
case DIREITA:
head.y += speed.y;
break;
case ESQUERDA:
head.y -= speed.y;
break;
case CIMA:
head.x += speed.x;
break;
case BAIXO:
head.x -= speed.x;
break;
}
}
/* generator radnnom postion food */
int RandomNumber(int high)
{
return (rand() % (high));
}
/* initiation postion food */
void foodint()
{
food.x = RandomNumber(600);
food.y = RandomNumber(400);
}
/* draw food */
SDL_Rect InitFood(SDL_Renderer * renderer)
{
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0xFF);
food.posLivre.x = food.x;
food.posLivre.y = food.y;
food.posLivre.w = SIZE;
food.posLivre.h = SIZE;
SDL_RenderFillRect(renderer, &food.posLivre);
return food.posLivre;
}
bool CheckCollision(SDL_Rect r1, SDL_Rect r2)
{
if(r1.x > r2.x+r2.w){return false;}
else if(r1.y > r2.y+r2.h){ return false; }
else if(r1.x+r1.w < r2.x){ return false; }
else if(r1.y+r1.h < r2.y){ return false;}
else{
return true;
}
}
void player(SDL_Renderer * renderer)
{ /* this func draw rectangle of PLAYER in post
hit.x hit.y and width 50 height 10 */
SDL_Rect hit;
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 0xFF);
hit.w = 10;
hit.h = 10;
snake.segmento[0].x = head.x;
snake.segmento[0].y = head.y;
snake.segmento[0].w = SIZE;
snake.segmento[0].h = SIZE;
for (int i = snake.totalSegmento; i > 0; i--)
{
snake.segmento[i] = snake.segmento[i - 1];
}
if (CheckCollision(snake.segmento[0], food.posLivre ))
{
snake.totalSegmento++;
foodint();
}
for (int i = snake.totalSegmento; i > 0; i--)
{
/* SDL_RenderCopy(renderer, snake.segmentTexture, &snake.sprite[0],
&snake.segmento[i]); */
SDL_RenderFillRect(renderer, &snake.segmento[i]);
} /* check collision with ball and player */
if (head.x > SCREEN_WIDTH)
{
head.x = 0;
}
if (head.y > SCREEN_HEIGHT)
{
head.y = 0;
}
if (head.x < 0)
{
head.x = SCREEN_WIDTH;
}
if (head.y < 0)
{
head.y = SCREEN_HEIGHT;
}
}
bool InitWindow()
{
int imgFlag = IMG_INIT_PNG;
IMG_Init(imgFlag);
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("ERRO\n");
return false;
}
else
{
window =
SDL_CreateWindow("Snake", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_OPENGL);
if (window == NULL)
{
printf("ERRO\n");
return false;
}
else
{
renderer =
SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
}
}
if ((backgroundImage = SDL_LoadBMP("img/bg.bmp")) == NULL)
printf("ERRO %s", SDL_GetError());
background = SDL_CreateTextureFromSurface(renderer, backgroundImage);
return true;
}
bool GetKeys()
{
while (SDL_PollEvent(&event) != 0)
{
if (event.type == SDL_QUIT)
{
return 1;
}
if (event.type == SDL_KEYDOWN)
{
switch (event.key.keysym.sym)
{
case SDLK_UP:
if (direcao != DIREITA)
direcao = ESQUERDA;
break;
case SDLK_DOWN:
if (direcao != ESQUERDA)
direcao = DIREITA;
break;
case SDLK_RIGHT:
if (direcao != BAIXO)
direcao = CIMA;
break;
case SDLK_LEFT:
if (direcao != CIMA)
direcao = BAIXO;
break;
}
}
}
return 0;
}
void GameLoop(bool & quit)
{
quit = GetKeys();
SDL_RenderClear(renderer);
go();
// foodint();
SDL_RenderCopy(renderer, background, NULL, NULL);
InitFood(renderer);
player(renderer);
SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderPresent(renderer);
}
void Close()
{
SDL_DestroyWindow(window);
window = NULL;
SDL_DestroyTexture(background);
SDL_FreeSurface(backgroundImage);
SDL_DestroyRenderer(renderer);
IMG_Quit();
SDL_Quit();
}
int main(int argc, char *argv[])
{
bool quit = false;
Uint32 frameStart, frameTime;
InitWindow();
while (!quit)
{
GameLoop(quit);
}
Close();
return 0;
}
No comments:
Post a Comment