Last active
September 4, 2017 09:20
-
-
Save ander94lakx/676056993c37770ba03444d4e4ae082c to your computer and use it in GitHub Desktop.
PruebasSDL2
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "Funtzioak.h" | |
#include <stdio.h> | |
#include <time.h> | |
#include <stdlib.h> | |
void inicializar() | |
{ | |
double pi = 3.1415926535; | |
int NumGrado = rand() % 360; | |
double NumRad = (NumGrado*pi) / 180; | |
// PELOTA | |
pelota.rectangulo.w = 5; | |
pelota.rectangulo.h = 5; | |
pelota.rectangulo.x = WIN_ANCHURA / 2 - pelota.rectangulo.w / 2; | |
pelota.rectangulo.y = WIN_ALTURA / 2 - pelota.rectangulo.h / 2; | |
pelota.direccion.x = cos(NumRad); | |
pelota.direccion.y = sin(NumRad); | |
pelota.velocidad = 10; | |
// PALAS | |
PalaD.rectangulo.w = 10; | |
PalaD.rectangulo.h = 40; | |
PalaD.rectangulo.y = WIN_ALTURA / 2 - PalaD.rectangulo.h / 2; | |
PalaD.rectangulo.x = WIN_ANCHURA - (PalaD.rectangulo.w + 5); | |
PalaD.velocidad = 10; | |
PalaI.rectangulo.w = 10; | |
PalaI.rectangulo.h = 40; | |
PalaI.rectangulo.y = WIN_ALTURA / 2 - PalaD.rectangulo.h / 2; | |
PalaI.rectangulo.x = 5; | |
PalaI.velocidad = 10; | |
// MARCADOR | |
marcador.Fuente = TTF_OpenFont("consola.ttf", 24); | |
SDL_Color negro = { 0, 0, 0 }; | |
marcador.Color = negro; | |
marcador.rectangulo.w = 80; | |
marcador.rectangulo.h = 40; | |
marcador.rectangulo.y = 20; | |
marcador.rectangulo.x = WIN_ANCHURA / 2 - marcador.rectangulo.w / 2; | |
marcador.puntuacionDer = 0; | |
marcador.puntuacionIzq = 0; | |
} | |
void input(SDL_Event event) | |
{ | |
if (event.type == SDL_KEYDOWN) | |
{ | |
if (event.key.keysym.sym == SDLK_ESCAPE) | |
{ | |
SDL_Quit(); | |
exit(0); | |
} | |
if (event.key.keysym.sym == SDLK_UP) | |
{ | |
PalaD.direccion.y = -1; | |
} | |
if (event.key.keysym.sym == SDLK_DOWN) | |
{ | |
PalaD.direccion.y = 1; | |
} | |
if (event.key.keysym.sym == SDLK_w) | |
{ | |
PalaI.direccion.y = -1; | |
} | |
if (event.key.keysym.sym == SDLK_s) | |
{ | |
PalaI.direccion.y = 1; | |
} | |
} | |
else if (event.type == SDL_KEYUP) | |
{ | |
if (event.key.keysym.sym == SDLK_UP) | |
{ | |
PalaD.direccion.y = 0; | |
} | |
if (event.key.keysym.sym == SDLK_DOWN) | |
{ | |
PalaD.direccion.y = 0; | |
} | |
if (event.key.keysym.sym == SDLK_w) | |
{ | |
PalaI.direccion.y = 0; | |
} | |
if (event.key.keysym.sym == SDLK_s) | |
{ | |
PalaI.direccion.y = 0; | |
} | |
} | |
} | |
void update() | |
{ | |
PalaD.rectangulo.x += (PalaD.direccion.x)*(PalaD.velocidad); | |
PalaD.rectangulo.y += (PalaD.direccion.y)*(PalaD.velocidad); | |
PalaI.rectangulo.x += (PalaI.direccion.x)*(PalaI.velocidad); | |
PalaI.rectangulo.y += (PalaI.direccion.y)*(PalaI.velocidad); | |
pelota.rectangulo.x += (pelota.direccion.x)*(pelota.velocidad); | |
pelota.rectangulo.y += (pelota.direccion.y)*(pelota.velocidad); | |
if (PalaD.rectangulo.y < 0) | |
{ | |
PalaD.direccion.y = 0; | |
PalaD.rectangulo.y = 0; | |
} | |
else if (PalaD.rectangulo.y > WIN_ALTURA - PalaD.rectangulo.h) | |
{ | |
PalaD.direccion.y = 0; | |
PalaD.rectangulo.y = WIN_ALTURA - PalaD.rectangulo.h; | |
} | |
if (PalaI.rectangulo.y < 0) | |
{ | |
PalaI.direccion.y = 0; | |
PalaI.rectangulo.y = 0; | |
} | |
else if (PalaI.rectangulo.y > WIN_ALTURA - PalaI.rectangulo.h) | |
{ | |
PalaI.direccion.y = 0; | |
PalaI.rectangulo.y = WIN_ALTURA - PalaI.rectangulo.h; | |
} | |
} | |
void render(SDL_Renderer* renderer) | |
{ | |
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); | |
SDL_RenderClear(renderer); | |
SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | |
SDL_RenderFillRect(renderer, &(pelota.rectangulo)); | |
SDL_RenderFillRect(renderer, &(PalaD.rectangulo)); | |
SDL_RenderFillRect(renderer, &(PalaI.rectangulo)); | |
char resultado[10]; | |
sprintf(resultado, "%d - %d", marcador.puntuacionIzq, marcador.puntuacionDer); | |
SDL_RenderFillRect(renderer, &(marcador.rectangulo)); | |
SDL_Surface* surfaceMessage = TTF_RenderText_Solid(marcador.Fuente, resultado, marcador.Color); | |
SDL_Texture* Message = SDL_CreateTextureFromSurface(renderer, surfaceMessage); | |
SDL_RenderCopy(renderer, Message, NULL, &marcador.rectangulo); | |
SDL_RenderPresent(renderer); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef _FUNTZIOAK_H_ | |
#define _FUNTZIOAK_H_ | |
#define WIN_ALTURA 480 | |
#define WIN_ANCHURA 640 | |
#include <SDL.h> | |
#include <SDL_ttf.h> | |
typedef struct direccion { | |
double x; | |
double y; | |
} DIRECCION; | |
typedef struct pala { | |
SDL_Rect rectangulo; | |
DIRECCION direccion; | |
float velocidad; | |
} PALA; | |
typedef struct pelota { | |
SDL_Rect rectangulo; | |
DIRECCION direccion; | |
float velocidad; | |
} PELOTA; | |
typedef struct marcador { | |
TTF_Font* Fuente; | |
SDL_Color Color; | |
SDL_Rect rectangulo; | |
int puntuacionIzq; | |
int puntuacionDer; | |
} MARCADOR; | |
// ACTORES | |
PALA PalaD; | |
PALA PalaI; | |
PELOTA pelota; | |
MARCADOR marcador; | |
void inicializar(); | |
void input(SDL_Event event); | |
void update(); | |
void render(SDL_Renderer* renderer); | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#define _CRT_SECURE_NO_WARNINGS | |
#include <stdio.h> | |
#include <string.h> | |
#include <time.h> | |
#include <stdlib.h> | |
#include <SDL.h> | |
#include "Funtzioak.h" | |
int main() | |
{ | |
// Para controlar los Frames del juego | |
const int FPS = 30; // El juego se mostrará 30 veces cada segundo (FPS == Frames Per Second) | |
const int TiempoMinimoFrame = 1000 / FPS; // Se divide 1000 entre los FPS para hallar el minimo que tiene que durar | |
int TiempoInicioFrame, TiempoFinFrame; | |
// INICIALIZACION | |
if (SDL_Init(SDL_INIT_VIDEO) != 0 || TTF_Init() != 0) | |
{ | |
printf("Unable to initialize SDL: %s", SDL_GetError()); | |
return 1; | |
} | |
SDL_Window* window; | |
window = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIN_ANCHURA, WIN_ALTURA, 0); | |
SDL_Renderer* renderer; | |
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_SOFTWARE); | |
srand(time(NULL)); | |
inicializar(); | |
render(renderer); // Se llama una primera vez antes de arrancar para asi verlo justo antes de empezar el juego | |
// BUCLE PRINCIPAL | |
SDL_Event Event; | |
while (1) | |
{ | |
TiempoInicioFrame = SDL_GetTicks(); // Se halla el tiempo en el que se empieza a hacer una vuelta del bucle | |
while (SDL_PollEvent(&Event)) | |
{ | |
input(Event); | |
} | |
update(); | |
render(renderer); | |
// Volvemos a hallar el tiempo y le restamos el que hemos calculado ates | |
TiempoFinFrame = SDL_GetTicks(); | |
int DuracionFrame = TiempoFinFrame - TiempoInicioFrame; | |
if (DuracionFrame < TiempoMinimoFrame) | |
{ | |
SDL_Delay(TiempoMinimoFrame - DuracionFrame); // ESperamos el tiempo que falte para llegar al minimo | |
} | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment