Ready. Set. Go!
commit
da6bcec3cd
|
@ -0,0 +1,2 @@
|
|||
*.o
|
||||
nothing
|
|
@ -0,0 +1,14 @@
|
|||
OBJS = src/main.o src/player.o
|
||||
LIBS=$(shell pkg-config gl sdl2 --libs)
|
||||
CFLAGS=-Wall -Werror -std=c11 $(shell pkg-config gl sdl2 --cflags)
|
||||
|
||||
nothing: $(OBJS)
|
||||
cc $(CFLAGS) $(OBJS) -o nothing $(LIBS)
|
||||
|
||||
%.o: %.c
|
||||
cc $(CFLAGS) -c $< -o $@
|
||||
|
||||
.PHONY: clean
|
||||
|
||||
clean:
|
||||
rm -rf nothing $(OBJS)
|
|
@ -0,0 +1,12 @@
|
|||
with import <nixpkgs> {}; {
|
||||
nothingEnv = stdenv.mkDerivation {
|
||||
name = "nothing-env";
|
||||
buildInputs = [ stdenv
|
||||
gcc
|
||||
SDL2
|
||||
mesa
|
||||
pkgconfig
|
||||
];
|
||||
LD_LIBRARY_PATH="${mesa}/lib";
|
||||
};
|
||||
}
|
|
@ -0,0 +1,88 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include "./player.h"
|
||||
|
||||
#define SCREEN_WIDTH 800
|
||||
#define SCREEN_HEIGHT 600
|
||||
#define GAME_FPS 60
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int exit_code = 0;
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
fprintf(stderr, "Could not initialize SDL: %s", SDL_GetError());
|
||||
exit_code = -1;
|
||||
goto sdl_init_fail;
|
||||
}
|
||||
|
||||
SDL_Window *window = SDL_CreateWindow("Nothing",
|
||||
100, 100,
|
||||
SCREEN_WIDTH, SCREEN_HEIGHT,
|
||||
SDL_WINDOW_SHOWN);
|
||||
if (window == NULL) {
|
||||
fprintf(stderr, "Could not create SDL window: %s", SDL_GetError());
|
||||
exit_code = -1;
|
||||
goto sdl_create_window_fail;
|
||||
}
|
||||
|
||||
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1,
|
||||
SDL_RENDERER_ACCELERATED |
|
||||
SDL_RENDERER_PRESENTVSYNC);
|
||||
if (renderer == NULL) {
|
||||
fprintf(stderr, "Could not create SDL renderer: %s", SDL_GetError());
|
||||
exit_code = -1;
|
||||
goto sdl_create_renderer_fail;
|
||||
}
|
||||
|
||||
struct player *player = create_player(0.0f, 200.0f);
|
||||
if (player == NULL) {
|
||||
perror("Could not create player");
|
||||
exit_code = -1;
|
||||
goto create_player_fail;
|
||||
}
|
||||
|
||||
int quit = 0;
|
||||
|
||||
const Uint8 *keyboard_state = SDL_GetKeyboardState(NULL);
|
||||
const int delay_ms = roundf(1000.0 / GAME_FPS);
|
||||
SDL_Event e;
|
||||
while (!quit) {
|
||||
while (SDL_PollEvent(&e)) {
|
||||
switch (e.type) {
|
||||
case SDL_QUIT:
|
||||
quit = 1;
|
||||
break;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
if (keyboard_state[SDL_SCANCODE_A]) {
|
||||
player_move_left(player);
|
||||
} else if (keyboard_state[SDL_SCANCODE_D]) {
|
||||
player_move_right(player);
|
||||
} else {
|
||||
player_stop(player);
|
||||
}
|
||||
|
||||
update_player(player, delay_ms);
|
||||
|
||||
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
|
||||
SDL_RenderClear(renderer);
|
||||
render_player(player, renderer);
|
||||
SDL_RenderPresent(renderer);
|
||||
SDL_Delay(delay_ms);
|
||||
}
|
||||
|
||||
destroy_player(player);
|
||||
create_player_fail:
|
||||
SDL_DestroyRenderer(renderer);
|
||||
sdl_create_renderer_fail:
|
||||
SDL_DestroyWindow(window);
|
||||
sdl_create_window_fail:
|
||||
SDL_Quit();
|
||||
sdl_init_fail:
|
||||
return exit_code;
|
||||
}
|
|
@ -0,0 +1,78 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include "./player.h"
|
||||
|
||||
#define PLAYER_WIDTH 50.0f
|
||||
#define PLAYER_HEIGHT 50.0f
|
||||
#define PLAYER_SPEED 500.0f
|
||||
|
||||
struct player {
|
||||
float x, y;
|
||||
float dx, dy;
|
||||
};
|
||||
|
||||
struct player *create_player(float x, float y)
|
||||
{
|
||||
struct player *player = malloc(sizeof(struct player));
|
||||
|
||||
if (player == NULL) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
player->x = x;
|
||||
player->y = y;
|
||||
player->dx = 0.0f;
|
||||
player->dy = 0.0f;
|
||||
|
||||
return player;
|
||||
}
|
||||
|
||||
void destroy_player(struct player * player)
|
||||
{
|
||||
free(player);
|
||||
}
|
||||
|
||||
int render_player(const struct player * player,
|
||||
SDL_Renderer *renderer)
|
||||
{
|
||||
if (SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
SDL_Rect rect;
|
||||
rect.x = roundf(player->x);
|
||||
rect.y = roundf(player->y);
|
||||
rect.w = roundf(PLAYER_WIDTH);
|
||||
rect.h = roundf(PLAYER_HEIGHT);
|
||||
|
||||
if (SDL_RenderFillRect(renderer, &rect) < 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void update_player(struct player * player, int delta_time)
|
||||
{
|
||||
float d = delta_time / 1000.0;
|
||||
|
||||
player->x += player->dx * d;
|
||||
player->y += player->dy * d;
|
||||
}
|
||||
|
||||
void player_move_left(struct player *player)
|
||||
{
|
||||
player->dx = -PLAYER_SPEED;
|
||||
}
|
||||
|
||||
void player_move_right(struct player *player)
|
||||
{
|
||||
player->dx = PLAYER_SPEED;
|
||||
}
|
||||
|
||||
void player_stop(struct player *player)
|
||||
{
|
||||
player->dx = 0.0f;
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
#ifndef PLAYER_H_
|
||||
#define PLAYER_H_
|
||||
|
||||
struct player;
|
||||
struct SDL_Renderer;
|
||||
|
||||
struct player *create_player(float x, float y);
|
||||
void destroy_player(struct player * player);
|
||||
|
||||
int render_player(const struct player * player,
|
||||
SDL_Renderer *renderer);
|
||||
void update_player(struct player * player, int delta_time);
|
||||
|
||||
void player_move_left(struct player *player);
|
||||
void player_move_right(struct player *player);
|
||||
void player_stop(struct player *player);
|
||||
|
||||
#endif // PLAYER_H_
|
Loading…
Reference in New Issue