diff options
| author | Thomas Guillermo Albers Raviola <thomas@thomaslabs.org> | 2026-01-16 19:38:33 +0100 |
|---|---|---|
| committer | Thomas Guillermo Albers Raviola <thomas@thomaslabs.org> | 2026-01-16 19:38:33 +0100 |
| commit | a90db3b7b6e87e24c789b5db222f1cef92809bde (patch) | |
| tree | 2096abcb1ac1ea970a51e598257186bc4e030b22 | |
Initial commit
33 files changed, 1227 insertions, 0 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..e69de29 --- /dev/null +++ b/README.md diff --git a/original/Makefile b/original/Makefile new file mode 100644 index 0000000..14fcb0a --- /dev/null +++ b/original/Makefile @@ -0,0 +1,15 @@ +SRC=colisiones.c loadscreen.c main.c procesar.c render.c +OBJ=$(SRC:%.c=%.o) +TARGET=highnoon + +%.o : %.c + $(CC) -c $< -o $@ + +$(TARGET) : main.h $(OBJ) + $(CC) $(OBJ) -lSDL2 -lSDL2_image -o $@ + +all: $(TARGET) + +.PHONY: clean +clean: + rm -r $(OBJ) $(TARGET) diff --git a/original/colisiones.c b/original/colisiones.c new file mode 100644 index 0000000..52b69be --- /dev/null +++ b/original/colisiones.c @@ -0,0 +1,60 @@ +#include "main.h" + +void collisionDetect(GameState *game) +{ + for(int i = 0; i < NUM_CAJAS; i++) + { + float mw = game->man.w, mh = game->man.h; + float mx =game->man.x, my = game->man.y; + float bx = game->suelo[i].x, by = game->suelo[i].y, bw = game->suelo[i].w, bh = game->suelo[i].h; + if(mx+mw/2 > bx && mx+mw/2<bx+bw) + { + //are we bumping our head? + if(my < by+bh && my > by) + { + //correct y + game->man.y = by+bh; + my = by+bh; + + //bumped our head, stop any jump velocity + game->man.dy = 0; + } + } + if(mx+mw > bx && mx<bx+bw) + { + //are we landing on the ledge + if(my+mh > by && my < by) + { + //correct y + game->man.y = by-mh; + my = by-mh; + + //landed on this ledge, stop any jump velocity + game->man.dy = 0; + game->salto = 0; + } + } + + if(my+mh > by && my<by+bh) + { + //rubbing against right edge + if(mx < bx+bw && mx+mw > bx+bw) + { + //correct x + game->man.x = bx+bw; + mx = bx+bw; + + game->man.dx = 0; + } + //rubbing against left edge + else if(mx+mw > bx && mx < bx) + { + //correct x + game->man.x = bx-mw; + mx = bx-mw; + + game->man.dx = 0; + } + } + } +} diff --git a/original/fotos/ladrillo.png b/original/fotos/ladrillo.png Binary files differnew file mode 100644 index 0000000..17f0ca2 --- /dev/null +++ b/original/fotos/ladrillo.png diff --git a/original/fotos/vaquero.png b/original/fotos/vaquero.png Binary files differnew file mode 100644 index 0000000..df71140 --- /dev/null +++ b/original/fotos/vaquero.png diff --git a/original/loadscreen.c b/original/loadscreen.c new file mode 100644 index 0000000..069de93 --- /dev/null +++ b/original/loadscreen.c @@ -0,0 +1,17 @@ +#include "main.h" + +void loadScreen(GameState *gameState, SDL_Renderer *renderer) +{ + SDL_Surface *playerSurface = NULL; + SDL_Surface *ladrilloSurface = NULL; + playerSurface = IMG_Load("fotos/vaquero.png"); + ladrilloSurface = IMG_Load("fotos/ladrillo.png"); + if(playerSurface == NULL || ladrilloSurface == NULL) + { + printf("Algunas texturas no pudieron ser encontradas"); + } + gameState->player = SDL_CreateTextureFromSurface(renderer, playerSurface); + gameState->ladrillo = SDL_CreateTextureFromSurface(renderer, ladrilloSurface); + SDL_FreeSurface(playerSurface); + SDL_FreeSurface(ladrilloSurface); +} diff --git a/original/main.c b/original/main.c new file mode 100644 index 0000000..1bd96fb --- /dev/null +++ b/original/main.c @@ -0,0 +1,92 @@ +#include "main.h" + +void gravity(GameState *gameState) +{ + gameState->man.y += gameState->man.dy; + gameState->man.dy += GRAVEDAD; +} + +int main(int argc, char *argv[]) +{ + GameState gameState; + SDL_Window *window = NULL; // Declare a window + SDL_Renderer *renderer = NULL; // Declare a renderer + SDL_Init(SDL_INIT_VIDEO); // Initialize SDL2 + gameState.man.x = 44; + gameState.man.y = 100; + gameState.man.dy = 0; + gameState.man.dx = 0; + gameState.man.w = 42; + gameState.man.h = 53; + gameState.salto = 0; + gameState.man.facing_left = 0; + + for(int i = 0; i < 3; i++) + { + gameState.camara[i].x = i*43 + 1; + gameState.camara[i].y = 1; + gameState.camara[i].w = gameState.man.w; + gameState.camara[i].h = gameState.man.h; + } + + for (int i; i < NUM_CAJAS; i++) + { + gameState.suelo[i].x = i*44; + gameState.suelo[i].y = 400; + gameState.suelo[i].w = 44; + gameState.suelo[i].h = 44; + } + gameState.suelo[9].x = 0; + gameState.suelo[9].y = 356; + gameState.suelo[8].x = 0; + gameState.suelo[8].y = 312; + + //Create an application window with the following settings: + window = SDL_CreateWindow("Game Window", // window title + SDL_WINDOWPOS_UNDEFINED, // initial x position + SDL_WINDOWPOS_UNDEFINED, // initial y position + 640, // width, in pixels + 480, // height, in pixels + 0 // flags + ); + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + if(window == NULL || renderer == NULL) + { + printf("SDL no pudo ejecutarse o no esta instalado"); + } + + gameState.renderer = renderer; + loadScreen(&gameState,renderer); + + // The window is open: enter program loop (see SDL_PollEvent) + int done = 0; + + //Event loop + while(!done) + { + //Check for events + done = processEvents(window, &gameState); + ///Ejecutar gravedad + gravity(&gameState); + ///Deteccion de Coliciones + collisionDetect(&gameState); + if(gameState.i / 3 >= 3) + gameState.i = 0; + //Render display + doRender(renderer, &gameState); + if(gameState.man.walking == 1) + { + gameState.i++; + gameState.man.walking = 0; + } + else + gameState.i = 0; + } + SDL_DestroyTexture(gameState.player); + // Close and destroy the window + SDL_DestroyWindow(window); + SDL_DestroyRenderer(renderer); + // Clean up + SDL_Quit(); + return 0; +} diff --git a/original/main.h b/original/main.h new file mode 100644 index 0000000..6074a1a --- /dev/null +++ b/original/main.h @@ -0,0 +1,50 @@ +#ifndef MAIN_H_INCLUDED +#define MAIN_H_INCLUDED +#include <SDL2/SDL.h> +#include <stdio.h> +#include <SDL2/SDL_image.h> +#define NUM_CAJAS 10 +#define GRAVEDAD 1 +#define CUADRO 0 + +typedef struct +{ + int x, y ,w, h; + +} Suelo; + +typedef struct +{ + int x, y, w, h; + int dy, dx; + int facing_left, walking; +} Man; + +typedef struct +{ + int x, y, w, h; +} Camara; + +typedef struct +{ + Man man; + Camara camara[3]; + Suelo suelo[NUM_CAJAS]; + SDL_Texture *player; + SDL_Texture *ladrillo; + SDL_Renderer *renderer; + int scrollx; + int scrolly; + int salto; + int i; +} GameState; + +int processEvents(SDL_Window *window, GameState *gameState); + +void doRender(SDL_Renderer *renderer, GameState *gameState); + +void loadScreen(GameState *gameState, SDL_Renderer *renderer); + +void collisionDetect(GameState *game); + +#endif // MAIN_H_INCLUDED diff --git a/original/procesar.c b/original/procesar.c new file mode 100644 index 0000000..2bb58b6 --- /dev/null +++ b/original/procesar.c @@ -0,0 +1,71 @@ +#include "main.h" + +int processEvents(SDL_Window *window,GameState *gameState) +{ + gameState->scrollx = -gameState->man.x; + SDL_Event event; + int done = 0; + + while(SDL_PollEvent(&event)) + { + switch(event.type) + { + case SDL_WINDOWEVENT_CLOSE: + { + if(window) + { + SDL_DestroyWindow(window); + window = NULL; + done = 1; + } + } + break; + case SDL_KEYDOWN: + { + switch(event.key.keysym.sym) + { + case SDLK_ESCAPE: + done = 1; + break; + } + } + break; + case SDL_QUIT: + //quit out of the game + done = 1; + break; + case SDL_MOUSEBUTTONDOWN: + gameState->man.x = event.button.x; + gameState->man.y = event.button.y; + break; + } + } + + const Uint8 *state = SDL_GetKeyboardState(NULL); + if(state[SDL_SCANCODE_LEFT]) + { + gameState->man.x -= 5; + gameState->man.facing_left = 1; + gameState->man.walking = 1; + } + if(state[SDL_SCANCODE_RIGHT]) + { + gameState->man.x += 5; + gameState->man.facing_left = 0; + gameState->man.walking = 1; + gameState->i++; + } + if(state[SDL_SCANCODE_SPACE]) + { + if(gameState->salto == 1 && gameState->man.dy != 0) + { + + } + else if(gameState->salto == 0 && gameState->man.dy == 0) + { + gameState->man.dy = -15; + gameState->salto = 1; + } + } + return done; +} diff --git a/original/render.c b/original/render.c new file mode 100644 index 0000000..ac02a57 --- /dev/null +++ b/original/render.c @@ -0,0 +1,33 @@ +#include "main.h" + +void doRender(SDL_Renderer *renderer, GameState *gameState) +{ + //set the drawing color to blue + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + + //Clear the screen (to blue) + SDL_RenderClear(renderer); + + //set the drawing color to white + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + ///Dibujar Persona + SDL_Rect figura = {gameState->man.x, gameState->man.y, gameState->man.w, gameState->man.h}; + + SDL_Rect persona = {gameState->camara[gameState->i / 3].x, gameState->camara[gameState->i / 3].y, gameState->camara[0].w, gameState->camara[0].h}; + if (gameState->man.facing_left == 0) + { + SDL_RenderCopyEx(renderer, gameState->player, &persona, &figura, 0 , NULL, SDL_FLIP_NONE); + } + else if (gameState->man.facing_left == 1) + { + SDL_RenderCopyEx(renderer, gameState->player, &persona, &figura, 0 , NULL, SDL_FLIP_HORIZONTAL); + } + ///Dibujar el ladrillo + for(int i = 0; i < NUM_CAJAS; i++) + { + SDL_Rect bloque = {gameState->suelo[i].x, gameState->suelo[i].y, gameState->suelo[i].w, gameState->suelo[i].h}; + SDL_RenderCopy(renderer, gameState->ladrillo, NULL, &bloque); + } + ///We are done drawing, "present" or show to the screen what we've drawn + SDL_RenderPresent(renderer); +} diff --git a/progress/Makefile b/progress/Makefile new file mode 100644 index 0000000..37166bb --- /dev/null +++ b/progress/Makefile @@ -0,0 +1,12 @@ +TARGET=highnoon +SRC=$(wildcard src/*.c) +OBJ=$(addprefix obj/, $(SRC:src/%.c=%.o)) +LDFLAGS=-lSDL2 -lSDL2_mixer -lSDL2_image -lSDL2_ttf + +obj/%.o : src/%.c + $(CC) -c $< -o $@ + +$(TARGET) : src/main.h $(OBJ) + $(CC) $(OBJ) -o $@ $(LDFLAGS) + +all: $(TARGET) diff --git a/progress/audio/arma.wav b/progress/audio/arma.wav Binary files differnew file mode 100644 index 0000000..45eb197 --- /dev/null +++ b/progress/audio/arma.wav diff --git a/progress/duel.map b/progress/duel.map new file mode 100644 index 0000000..987720f --- /dev/null +++ b/progress/duel.map @@ -0,0 +1,3 @@ +2 +0 0 20 20 +100 100 20 20
\ No newline at end of file diff --git a/progress/font/Montague.ttf b/progress/font/Montague.ttf Binary files differnew file mode 100644 index 0000000..1365e34 --- /dev/null +++ b/progress/font/Montague.ttf diff --git a/progress/fotos/bullet.png b/progress/fotos/bullet.png Binary files differnew file mode 100644 index 0000000..3d032f1 --- /dev/null +++ b/progress/fotos/bullet.png diff --git a/progress/fotos/fondo1.png b/progress/fotos/fondo1.png Binary files differnew file mode 100644 index 0000000..772be84 --- /dev/null +++ b/progress/fotos/fondo1.png diff --git a/progress/fotos/gameover.png b/progress/fotos/gameover.png Binary files differnew file mode 100644 index 0000000..e18a7cf --- /dev/null +++ b/progress/fotos/gameover.png diff --git a/progress/fotos/ladrillo.png b/progress/fotos/ladrillo.png Binary files differnew file mode 100644 index 0000000..17f0ca2 --- /dev/null +++ b/progress/fotos/ladrillo.png diff --git a/progress/fotos/menu.png b/progress/fotos/menu.png Binary files differnew file mode 100644 index 0000000..4cace47 --- /dev/null +++ b/progress/fotos/menu.png diff --git a/progress/fotos/vaquero.png b/progress/fotos/vaquero.png Binary files differnew file mode 100644 index 0000000..df71140 --- /dev/null +++ b/progress/fotos/vaquero.png diff --git a/progress/fotos/vaquero2.png b/progress/fotos/vaquero2.png Binary files differnew file mode 100644 index 0000000..deaa22a --- /dev/null +++ b/progress/fotos/vaquero2.png diff --git a/progress/src/bulletCollision.c b/progress/src/bulletCollision.c new file mode 100644 index 0000000..d6c52c5 --- /dev/null +++ b/progress/src/bulletCollision.c @@ -0,0 +1,31 @@ +#include "main.h" + +void bulletCollision(GameState *gameState) +{ + int p1_x = gameState->player1.x, p1_y = gameState->player1.y; + int p1_w = gameState->player1.w, p1_h = gameState->player1.h; + + int p2_x = gameState->player2.x, p2_y = gameState->player2.y; + int p2_w = gameState->player2.w, p2_h = gameState->player2.h; + + for(int i = 0; i < MAX_BULLETS; i++) if(gameState->bala_1[i]) + { + if(gameState->bala_1[i]->x > p1_x && gameState->bala_1[i]->x < p1_x + p1_w + && gameState->bala_1[i]->y > p1_y && gameState->bala_1[i]->y < p1_y + p1_h && gameState->bala_1[i]->owner == 2) + { + if(gameState->player1.muerto == 1){} + else + printf("player1 muerto\n"); + gameState->player1.muerto = 1; + } + else if(gameState->bala_1[i]->x > p2_x && gameState->bala_1[i]->x < p2_x + p2_w + && gameState->bala_1[i]->y > p2_y && gameState->bala_1[i]->y < p2_y + p2_h && gameState->bala_1[i]->owner == 1) + { + if(gameState->player2.muerto == 1){} + else + printf("player2 muerto\n"); + gameState->player2.muerto = 1; + + } + } +} diff --git a/progress/src/colisiones.c b/progress/src/colisiones.c new file mode 100644 index 0000000..455db40 --- /dev/null +++ b/progress/src/colisiones.c @@ -0,0 +1,150 @@ +#include "main.h" + +void collisionDetect(GameState *gameState) +{ + ///Player 1 + if(gameState->player1.y >= 420) + { + gameState->player1.y = 420; + gameState->player1.dy = 0; + gameState->player1.salto = 0; + } + if(gameState->player1.x <= 0) + { + gameState->player1.x = 0; + gameState->player1.dx = 0; + } + if(gameState->player1.x >= 600) + { + gameState->player1.x = 600; + gameState->player1.dx = 0; + } + for(int i = 0; i < gameState->maxE; i++) + { + float mw = gameState->player1.w, mh = gameState->player1.h; + float mx =gameState->player1.x, my = gameState->player1.y; + float bx = gameState->entidades[i].x, by = gameState->entidades[i].y, bw = gameState->entidades[i].w, bh = gameState->entidades[i].h; + if(mx+mw/2 > bx && mx+mw/2<bx+bw) + { + //are we bumping our head? + if(my < by+bh && my > by) + { + //correct y + gameState->player1.y = by+bh; + my = by+bh; + + //bumped our head, stop any jump velocity + gameState->player1.dy = 0; + } + } + if(mx+mw > bx && mx<bx+bw) + { + //are we landing on the ledge + if(my+mh > by && my < by) + { + //correct y + gameState->player1.y = by-mh; + my = by-mh; + + //landed on this ledge, stop any jump velocity + gameState->player1.dy = 0; + gameState->player1.salto = 0; + } + } + + if(my+mh > by && my<by+bh) + { + //rubbing against right edge + if(mx < bx+bw && mx+mw > bx+bw) + { + //correct x + gameState->player1.x = bx+bw; + mx = bx+bw; + + gameState->player1.dx = 0; + } + //rubbing against left edge + else if(mx+mw > bx && mx < bx) + { + //correct x + gameState->player1.x = bx-mw; + mx = bx-mw; + + gameState->player1.dx = 0; + } + } + } + + ///Player 2 + if(gameState->player2.y >= 420) + { + gameState->player2.y = 420; + gameState->player2.dy = 0; + gameState->player2.salto = 0; + } + if(gameState->player2.x <= 0) + { + gameState->player2.x = 0; + gameState->player2.dx = 0; + } + if(gameState->player2.x >= 600) + { + gameState->player2.x = 600; + gameState->player2.dx = 0; + } + for(int i = 0; i < gameState->maxE; i++) + { + float mw = gameState->player2.w, mh = gameState->player2.h; + float mx =gameState->player2.x, my = gameState->player2.y; + float bx = gameState->entidades[i].x, by = gameState->entidades[i].y, bw = gameState->entidades[i].w, bh = gameState->entidades[i].h; + if(mx+mw/2 > bx && mx+mw/2<bx+bw) + { + //are we bumping our head? + if(my < by+bh && my > by) + { + //correct y + gameState->player2.y = by+bh; + my = by+bh; + + //bumped our head, stop any jump velocity + gameState->player2.dy = 0; + } + } + if(mx+mw > bx && mx<bx+bw) + { + //are we landing on the ledge + if(my+mh > by && my < by) + { + //correct y + gameState->player2.y = by-mh; + my = by-mh; + + //landed on this ledge, stop any jump velocity + gameState->player2.dy = 0; + gameState->player2.salto = 0; + } + } + + if(my+mh > by && my<by+bh) + { + //rubbing against right edge + if(mx < bx+bw && mx+mw > bx+bw) + { + //correct x + gameState->player2.x = bx+bw; + mx = bx+bw; + + gameState->player2.dx = 0; + } + //rubbing against left edge + else if(mx+mw > bx && mx < bx) + { + //correct x + gameState->player2.x = bx-mw; + mx = bx-mw; + + gameState->player2.dx = 0; + } + } + } +} diff --git a/progress/src/declaraciones.c b/progress/src/declaraciones.c new file mode 100644 index 0000000..c23223a --- /dev/null +++ b/progress/src/declaraciones.c @@ -0,0 +1,62 @@ + #include "main.h" + + void declaraciones(GameState *gameState) + { + ///Declaracion del Player1 + gameState->player1.x = 44; + gameState->player1.y = 100; + gameState->player1.dy = 0; + gameState->player1.dx = 0; + gameState->player1.w = 42; + gameState->player1.h = 53; + gameState->player1.salto = 0; + gameState->player1.facing_left = 0; + gameState->player1.camara = 0; + gameState->player1.shooting = 0; + gameState->player1.muerto = 0; + for(int i = 0; i < 3; i++) + { + gameState->player1.camara_x[i] = i*43 + 1; + gameState->player1.camara_y[i] = 1; + } + + ///Declaracion del Player2 + gameState->player2.x = 44; + gameState->player2.y = 100; + gameState->player2.dy = 0; + gameState->player2.dx = 0; + gameState->player2.w = 42; + gameState->player2.h = 53; + gameState->player2.salto = 0; + gameState->player2.facing_left = 0; + gameState->player2.camara = 0; + gameState->player2.shooting = 0; + gameState->player2.muerto = 0; + for(int i = 0; i < 3; i++) + { + gameState->player2.camara_x[i] = i*43 + 1; + gameState->player2.camara_y[i] = 1; + } + + ///Declaracion de las bala_1 + for(int i = 0; i < MAX_BULLETS; i++) + { + gameState->bala_1[i] = NULL; + } + gameState->player1.cargador = MAX_BULLETS; + gameState->player2.cargador = MAX_BULLETS; + + ///Declaraciones del Menu + gameState->menu.x = 0; + gameState->menu.y = 0; + gameState->menu.w = 640; + gameState->menu.h = 480; + gameState->menu.camara_x[0] = 1; + gameState->menu.camara_x[1] = 66; + gameState->menu.camara_x[2] = 131; + gameState->menu.camara_y = 1; + gameState->menu.camara = 0; + + ///Cargar Mapa + loadGame(gameState); + } diff --git a/progress/src/gameprogress.c b/progress/src/gameprogress.c new file mode 100644 index 0000000..2fe614a --- /dev/null +++ b/progress/src/gameprogress.c @@ -0,0 +1,24 @@ +#include "main.h" + +void loadGame(GameState *gameState) +{ + FILE *fp; + fp = fopen ("duel.map", "r"); + int x; + int y; + int w; + int h; + fscanf(fp,"%d", &gameState->maxE); + for(int i = 0; i < gameState->maxE; i++) + { + fscanf(fp,"%d", &x); + fscanf(fp,"%d", &y); + fscanf(fp,"%d", &w); + fscanf(fp,"%d", &h); + gameState->entidades[i].x = x; + gameState->entidades[i].y = y; + gameState->entidades[i].w = w; + gameState->entidades[i].h = h; + printf("%d\n", gameState->entidades[i].x); + } +} diff --git a/progress/src/loadscreen.c b/progress/src/loadscreen.c new file mode 100644 index 0000000..c2395cd --- /dev/null +++ b/progress/src/loadscreen.c @@ -0,0 +1,35 @@ +#include "main.h" + +void loadScreen(GameState *gameState, SDL_Renderer *renderer) +{ + SDL_Surface *player1Surface = NULL; + SDL_Surface *menuSurface = NULL; + SDL_Surface *balaSurface = NULL; + SDL_Surface *player2Surface = NULL; + SDL_Surface *gameOverSurface = NULL; + SDL_Surface *backGroundSurface = NULL; + player1Surface = IMG_Load("fotos/vaquero.png"); + player2Surface = IMG_Load("fotos/vaquero2.png"); + menuSurface = IMG_Load("fotos/menu.png"); + balaSurface = IMG_Load("fotos/bullet.png"); + gameOverSurface = IMG_Load("fotos/gameover.png"); + backGroundSurface = IMG_Load("fotos/fondo1.png"); + + ///Load Sounds + gameState->disparo = Mix_LoadWAV("audio/arma.wav"); + Mix_VolumeChunk(gameState->disparo, 32); + + gameState->font = TTF_OpenFont("font/Montague.ttf", 48); + gameState->player1Texture = SDL_CreateTextureFromSurface(renderer, player1Surface); + gameState->player2Texture = SDL_CreateTextureFromSurface(renderer, player2Surface); + gameState->menuTexture = SDL_CreateTextureFromSurface(renderer, menuSurface); + gameState->balaTexture = SDL_CreateTextureFromSurface(renderer, balaSurface); + gameState->gameOver = SDL_CreateTextureFromSurface(renderer, gameOverSurface); + gameState->backGround = SDL_CreateTextureFromSurface(renderer, backGroundSurface); + SDL_FreeSurface(player1Surface); + SDL_FreeSurface(player2Surface); + SDL_FreeSurface(menuSurface); + SDL_FreeSurface(balaSurface); + SDL_FreeSurface(gameOverSurface); + SDL_FreeSurface(backGroundSurface); +} diff --git a/progress/src/main.c b/progress/src/main.c new file mode 100644 index 0000000..04dfc1a --- /dev/null +++ b/progress/src/main.c @@ -0,0 +1,146 @@ +#include "main.h" +#define FRAME_VALUES 10 + +void gravity(GameState *gameState) +{ + gameState->player1.y += gameState->player1.dy; + gameState->player1.dy += GRAVEDAD; + //gameState->player2.y += gameState->player2.dy; + //gameState->player2.dy += GRAVEDAD; +} + +Uint32 frametimes[FRAME_VALUES]; + +Uint32 frametimelast; + +Uint32 framecount; + +float framespersecond; + +void fpsinit() { + memset(frametimes, 0, sizeof(frametimes)); + framecount = 0; + framespersecond = 0; + frametimelast = SDL_GetTicks(); + +} + +void fpsthink() { + + Uint32 frametimesindex; + Uint32 getticks; + Uint32 count; + Uint32 i; + + frametimesindex = framecount % FRAME_VALUES; + + getticks = SDL_GetTicks(); + + frametimes[frametimesindex] = getticks - frametimelast; + + frametimelast = getticks; + + framecount++; + + if (framecount < FRAME_VALUES) { + + count = framecount; + + } else { + + count = FRAME_VALUES; + + } + + framespersecond = 0; + for (i = 0; i < count; i++) { + + framespersecond += frametimes[i]; + + } + + framespersecond /= count; + + framespersecond = 1000.f / framespersecond; + +} + + +int main() +{ + GameState gameState; + SDL_Window *window = NULL; // Declare a window + SDL_Renderer *renderer = NULL; // Declare a renderer + SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO); // Initialize SDL2 + + declaraciones(&gameState); + + //Create an application window with the following settings: + window = SDL_CreateWindow("HighNoon", // window title + SDL_WINDOWPOS_UNDEFINED, // initial x position + SDL_WINDOWPOS_UNDEFINED, // initial y position + 640, // width, in pixels + 480, // height, in pixels + 0 // flags + ); + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);// | SDL_RENDERER_PRESENTVSYNC); + + if(window == NULL || renderer == NULL) + { + printf("SDL no pudo ejecutarse o no esta instalado"); + } + TTF_Init(); + gameState.renderer = renderer; + Mix_OpenAudio(MIX_DEFAULT_FREQUENCY, MIX_DEFAULT_FORMAT, MIX_DEFAULT_CHANNELS, 4096); + loadScreen(&gameState,renderer); + // The window is open: enter program loop (see SDL_PollEvent) + int menu_done = 0; + gameState.done = 0; + while(!menu_done) + { + menu_done = menu_events(window,&gameState); + menu_render(renderer, &gameState); + } + + fpsinit(); + ///Event loop + while(!gameState.done) + { + ///Check for events + gameState.done = processEvents(window, &gameState); + ///Ejecutar gravedad + gravity(&gameState); + ///Deteccion de Coliciones (Objetos) + collisionDetect(&gameState); + ///Render display + doRender(renderer, &gameState); + ///Trabaja los Sprites + spriteUpdate(&gameState); + ///Balas + processBullet(&gameState); + ///Deteccion de Coliciones (Balas) + bulletCollision(&gameState); + fpsthink(); + printf("%f\n", framespersecond); + } + ///Limpia balas + for(int i = 0; i < MAX_BULLETS; i++) + removeBullet(i, &gameState); + + SDL_DestroyTexture(gameState.player1Texture); + SDL_DestroyTexture(gameState.player2Texture); + SDL_DestroyTexture(gameState.ladrilloTexture); + SDL_DestroyTexture(gameState.balaTexture); + SDL_DestroyTexture(gameState.menuTexture); + SDL_DestroyTexture(gameState.gameOver); + SDL_DestroyTexture(gameState.backGround); + Mix_FreeChunk(gameState.disparo); + TTF_CloseFont(gameState.font); + // Close and destroy the window + SDL_DestroyWindow(window); + SDL_DestroyRenderer(renderer); + TTF_Quit(); + // Clean up + SDL_Quit(); + return 0; +} diff --git a/progress/src/main.h b/progress/src/main.h new file mode 100644 index 0000000..19bd3ca --- /dev/null +++ b/progress/src/main.h @@ -0,0 +1,92 @@ +#ifndef MAIN_H_INCLUDED +#define MAIN_H_INCLUDED +#include "SDL2/SDL.h" +#include <stdio.h> +#include <SDL2/SDL_image.h> +#include <SDL2/SDL_mixer.h> +#include <SDL2/SDL_ttf.h> + +#define GRAVEDAD 1 +#define CUADRO 0 +#define MAX_BULLETS 6 +#define ENTIDADES 2 + + +typedef struct +{ + int x, y, w, h; + int dy, dx; + int facing_left, walking; + int camara_x[3], camara_y[3]; + int camara; + int salto; + int shooting; + int cargador; + int muerto; +} Player1; + +typedef struct +{ + int x, y, w, h; + int dy, dx; + int facing_left, walking; + int camara_x[3], camara_y[3]; + int camara; + int salto; + int shooting; + int cargador; + int muerto; +} Player2; + +typedef struct +{ + int x, y, w, h; + int camara_x[3], camara_y; + int camara; +} Menu; + +typedef struct +{ + int x, y, dx, owner; +} Bala_player_1; + +typedef struct +{ + int x, y, w, h; +} Entidades[ENTIDADES]; +typedef struct +{ + Player1 player1; + Player2 player2; + Menu menu; + Entidades entidades; + Bala_player_1 *bala_1[MAX_BULLETS]; + SDL_Texture *balaTexture; + SDL_Texture *player1Texture; + SDL_Texture *player2Texture; + SDL_Texture *ladrilloTexture; + SDL_Texture *vidaTexture; + SDL_Texture *menuTexture; + SDL_Texture *gameOver; + SDL_Texture *backGround; + SDL_Renderer *renderer; + Mix_Chunk *disparo; + TTF_Font *font; + int done; + int maxE; +} GameState; + +int processEvents(SDL_Window *window, GameState *gameState); +void doRender(SDL_Renderer *renderer, GameState *gameState); +int menu_events(SDL_Window *window,GameState *gameState); +void menu_render(SDL_Renderer *renderer, GameState *gameState); +void loadScreen(GameState *gameState, SDL_Renderer *renderer); +void collisionDetect(GameState *gameState); +int addBullet(int x, int y, int dx, GameState *gameState, int player); +void processBullet(GameState *gameState); +void removeBullet(int i, GameState *gameState); +void declaraciones(GameState *gameState); +void spriteUpdate(GameState *gameState); +void bulletCollision(GameState *gameState); +void loadGame(GameState *gameState); +#endif // MAIN_H_INCLUDED diff --git a/progress/src/menu.c b/progress/src/menu.c new file mode 100644 index 0000000..ea4b1df --- /dev/null +++ b/progress/src/menu.c @@ -0,0 +1,72 @@ +#include "main.h" + +void menu_render(SDL_Renderer *renderer, GameState *gameState) +{ + //set the drawing color to white + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + + //Clear the screen (to blue) + SDL_RenderClear(renderer); + ///set the drawing color to white + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + ///Dibujar Persona + SDL_Rect menu = {gameState->menu.x, gameState->menu.y, gameState->menu.w, gameState->menu.h}; + SDL_Rect menu_sprite = {gameState->menu.camara_x[gameState->menu.camara], gameState->menu.camara_y, 64, 48}; + if (gameState->player1.facing_left == 0) + SDL_RenderCopyEx(renderer, gameState->menuTexture, &menu_sprite, &menu, 0 , NULL, SDL_FLIP_NONE); + ///We are done drawing, "present" or show to the screen what we've drawn + SDL_RenderPresent(renderer); +} + +int menu_events(SDL_Window *window,GameState *gameState) +{ + SDL_Event event; + int done = 0; + while(SDL_PollEvent(&event)) + { + switch(event.type) + { + case SDL_WINDOWEVENT_CLOSE: + { + if(window) + { + SDL_DestroyWindow(window); + window = NULL; + done = 1; + } + } + break; + case SDL_KEYDOWN: + { + switch(event.key.keysym.sym) + { + case SDLK_ESCAPE: + done = 1; + break; + } + } + break; + case SDL_QUIT: + //quit out of the game + done = 1; + break; + case SDL_MOUSEBUTTONDOWN: + if(event.motion.x > 60 && event.motion.x < 580 && event.motion.y > 190 && event.motion.y < 270) + done = 1; + if(event.motion.x > 60 && event.motion.x < 580 && event.motion.y > 340 && event.motion.y < 420) + { + gameState->done = 1; + done = 1; + } + break; + case SDL_MOUSEMOTION: + if(event.motion.x > 60 && event.motion.x < 580 && event.motion.y > 190 && event.motion.y < 270) + gameState->menu.camara = 1; + else if(event.motion.x > 60 && event.motion.x < 580 && event.motion.y > 340 && event.motion.y < 420) + gameState->menu.camara = 2; + else + gameState->menu.camara = 0; + } + } + return done; +} diff --git a/progress/src/procesar.c b/progress/src/procesar.c new file mode 100644 index 0000000..aa7f104 --- /dev/null +++ b/progress/src/procesar.c @@ -0,0 +1,127 @@ +#include "main.h" + +int processEvents(SDL_Window *window,GameState *gameState) +{ + SDL_Event event; + int done = 0; + + while(SDL_PollEvent(&event)) + { + switch(event.type) + { + case SDL_WINDOWEVENT_CLOSE: + { + if(window) + { + SDL_DestroyWindow(window); + window = NULL; + done = 1; + } + } + break; + case SDL_KEYDOWN: + { + switch(event.key.keysym.sym) + { + case SDLK_ESCAPE: + done = 1; + break; + + case SDLK_e: + { + if(!gameState->player2.facing_left) + { + gameState->player2.shooting = addBullet(gameState->player2.x+35, gameState->player2.y+20, 7, gameState, 2); + } + else + { + gameState->player2.shooting = addBullet(gameState->player2.x+5, gameState->player2.y+20, -7, gameState, 2); + } + if(gameState->player2.shooting) + Mix_PlayChannel(-1, gameState->disparo, 0); + } + break; + } + } + break; + case SDL_QUIT: + //quit out of the game + done = 1; + break; + + case SDL_MOUSEBUTTONDOWN: + { + if(!gameState->player1.facing_left) + { + gameState->player1.shooting = addBullet(gameState->player1.x+35, gameState->player1.y+20, 7, gameState, 1); + } + else + { + gameState->player1.shooting = addBullet(gameState->player1.x+5, gameState->player1.y+20, -7, gameState, 1); + } + if(gameState->player1.shooting) + Mix_PlayChannel(-1, gameState->disparo, 0); + } + break; + } + } + + const Uint8 *state = SDL_GetKeyboardState(NULL); + if(state[SDL_SCANCODE_LEFT]) + { + gameState->player1.x -= 5; + gameState->player1.facing_left = 1; + gameState->player1.walking = 1; + } + if(state[SDL_SCANCODE_RIGHT]) + { + gameState->player1.x += 5; + gameState->player1.facing_left = 0; + gameState->player1.walking = 1; + } + if(state[SDL_SCANCODE_UP]) + { + if(gameState->player1.salto == 1 && gameState->player1.dy != 0) + { + + } + else if(gameState->player1.salto == 0 && gameState->player1.dy == 0) + { + gameState->player1.dy = -15; + gameState->player1.salto = 1; + } + } + if(state[SDL_SCANCODE_A]) + { + gameState->player2.x -= 5; + gameState->player2.facing_left = 1; + gameState->player2.walking = 1; + } + if(state[SDL_SCANCODE_D]) + { + gameState->player2.x += 5; + gameState->player2.facing_left = 0; + gameState->player2.walking = 1; + } + if(state[SDL_SCANCODE_W]) + { + if(gameState->player2.salto == 1 && gameState->player2.dy != 0) + { + + } + else if(gameState->player2.salto == 0 && gameState->player2.dy == 0) + { + gameState->player2.dy = -15; + gameState->player2.salto = 1; + } + } + if(state[SDL_SCANCODE_SPACE]) + { + gameState->player2.y -= 5; + } + if(state[SDL_SCANCODE_S]) + { + gameState->player2.y += 5; + } + return done; +} diff --git a/progress/src/render.c b/progress/src/render.c new file mode 100644 index 0000000..227b339 --- /dev/null +++ b/progress/src/render.c @@ -0,0 +1,45 @@ +#include "main.h" + +void doRender(SDL_Renderer *renderer, GameState *gameState) +{ + //set the drawing color to white + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + + //Clear the screen (to blue) + SDL_RenderClear(renderer); + ///set the drawing color to white + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + + ///Dibujar Escenario + SDL_Rect backGround = {0,0,640,480}; + SDL_RenderCopy(renderer, gameState->backGround, NULL, &backGround); + + ///Dibujar Player1 + SDL_Rect player1 = {gameState->player1.x, gameState->player1.y, gameState->player1.w, gameState->player1.h}; + SDL_Rect player1_sprite = {gameState->player1.camara_x[gameState->player1.camara / 3], gameState->player1.camara_y[gameState->player1.camara / 3], gameState->player1.w, gameState->player1.h}; + SDL_RenderCopyEx(renderer, gameState->player1Texture, &player1_sprite, &player1, 0 , NULL, gameState->player1.facing_left); + + ///Dibujar Player2 + SDL_Rect player2 = {gameState->player2.x, gameState->player2.y, gameState->player2.w, gameState->player2.h}; + SDL_Rect player2_sprite = {gameState->player2.camara_x[gameState->player2.camara / 3], gameState->player2.camara_y[gameState->player2.camara / 3], gameState->player2.w, gameState->player2.h}; + SDL_RenderCopyEx(renderer, gameState->player2Texture, &player2_sprite, &player2, 0 , NULL, gameState->player2.facing_left); + + ///Dibujar Balas + for(int i = 0; i < MAX_BULLETS; i++) + { + if(gameState->bala_1[i]) + { + SDL_Rect bala = { gameState->bala_1[i]->x, gameState->bala_1[i]->y, 8, 8}; + SDL_RenderCopy(renderer, gameState->balaTexture, NULL, &bala); + } + } + + if(gameState->player1.muerto == 1 || gameState->player2.muerto == 1) + { + ///Dibujar GameOver + SDL_Rect gameOver = {0,0,640,480}; + SDL_RenderCopy(renderer, gameState->gameOver, NULL, &gameOver); + } + ///We are done drawing, "present" or show to the screen what we've drawn + SDL_RenderPresent(renderer); +} diff --git a/progress/src/shoot.c b/progress/src/shoot.c new file mode 100644 index 0000000..4edb116 --- /dev/null +++ b/progress/src/shoot.c @@ -0,0 +1,57 @@ +#include "main.h" + +int addBullet(int x, int y, int dx, GameState *gameState, int player) +{ + int found = -1; + int shooting = 0; + for(int i = 0; i < MAX_BULLETS; i++) + { + if(gameState->bala_1[i] == NULL) + { + if(player == 1 && gameState->player1.cargador > 0) + { + found = i; + shooting = 1; + gameState->player1.cargador -= 1; + } + else if(player == 2 && gameState->player2.cargador > 0) + { + found = i; + shooting = 1; + gameState->player2.cargador -= 1; + } + printf("%d/6 - %d/6\n",gameState->player1.cargador, gameState->player2.cargador); + break; + } + } + + if(found >= 0) + { + int i = found; + gameState->bala_1[i] = malloc(sizeof(Bala_player_1)); + gameState->bala_1[i]->x = x; + gameState->bala_1[i]->y = y; + gameState->bala_1[i]->dx = dx; + gameState->bala_1[i]->owner = player; + } + return shooting; +} + +void removeBullet(int i, GameState *gameState) +{ + if(gameState->bala_1[i]) + { + free(gameState->bala_1[i]); + gameState->bala_1[i] = NULL; + } +} + +void processBullet(GameState *gameState) +{ + for(int i = 0; i < MAX_BULLETS; i++) if(gameState->bala_1[i]) + { + gameState->bala_1[i]->x += gameState->bala_1[i]->dx; + if(gameState->bala_1[i]->x < -1000 || gameState->bala_1[i]->x > 1000) + removeBullet(i, gameState); + } +} diff --git a/progress/src/spriteUpdate.c b/progress/src/spriteUpdate.c new file mode 100644 index 0000000..d91fe0e --- /dev/null +++ b/progress/src/spriteUpdate.c @@ -0,0 +1,33 @@ +#include "main.h" + +void spriteUpdate(GameState *gameState) +{ + ///Actualiza sprites + if(gameState->player1.walking == 1) + { + gameState->player1.camara++; + gameState->player1.walking = 0; + } + else + gameState->player1.camara = 0; + + if(gameState->player2.walking == 1) + { + gameState->player2.camara++; + gameState->player2.walking = 0; + } + else + gameState->player2.camara = 0; + + ///Reinicia sprites + if(gameState->player1.camara / 3 >= 3) + { + gameState->player1.camara = 0; + } + + if(gameState->player2.camara / 3 >= 3) + { + gameState->player2.camara = 0; + } +} + |
