#include <GLES/gl.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include <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;
#define CIMA 1
#define BAIXO 2
#define ESQUERDA 3
#define DIREITA 4
unsigned direcao = 0;
struct snake
{
/* head postion */
int x;
int y;
/*i try like this but i dont now*/
snake *boody;
} head =
{
0, 0}, speed =
{
2, 2};
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;
}
}
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;
hit.x = head.x;
hit.y = head.y;
SDL_RenderFillRect(renderer, &hit);
/* 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;
}
}
void color()
{
SDL_SetRenderDrawColor(renderer, 255, 0, 0, SDL_ALPHA_OPAQUE);
}
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);
/*
if ((snake.segmentImage = SDL_LoadBMP("img/segment.bmp")) == NULL)
printf("ERRO %s", SDL_GetError()); snake.segmentTexture =
SDL_CreateTextureFromSurface(renderer, snake.segmentImage);
if ((food.foodImage = SDL_LoadBMP("img/food.bmp")) == NULL)
printf("ERRO %s", SDL_GetError()); food.foodTexture =
SDL_CreateTextureFromSurface(renderer, food.foodImage);
if((pauseImage = IMG_Load("img/pause.png")) == NULL) printf("ERRO %s",
IMG_GetError()); pause = SDL_CreateTextureFromSurface(renderer,
pauseImage); */
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();
SDL_RenderCopy(renderer, background, NULL, NULL);
player(renderer);
color();
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;
InitWindow();
while (!quit)
{
GameLoop(quit);
}
Close();
return 0;
}
No comments:
Post a Comment