diff options
Diffstat (limited to 'original')
| -rw-r--r-- | original/Makefile | 15 | ||||
| -rw-r--r-- | original/colisiones.c | 60 | ||||
| -rw-r--r-- | original/fotos/ladrillo.png | bin | 0 -> 427 bytes | |||
| -rw-r--r-- | original/fotos/vaquero.png | bin | 0 -> 1018 bytes | |||
| -rw-r--r-- | original/loadscreen.c | 17 | ||||
| -rw-r--r-- | original/main.c | 92 | ||||
| -rw-r--r-- | original/main.h | 50 | ||||
| -rw-r--r-- | original/procesar.c | 71 | ||||
| -rw-r--r-- | original/render.c | 33 |
9 files changed, 338 insertions, 0 deletions
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); +} |
