Skip to content

Instantly share code, notes, and snippets.

View Mikepicker's full-sized avatar

Michele Rullo Mikepicker

  • Anzio, RM
View GitHub Profile
" Mike's statusline
let g:battery_level = ''
function! SetBatteryLevel(timer_id)
let l:battery_level = system('acpi | grep -oP "(\d+)%" | tr -d "\n"')
if (battery_level != '')
let g:battery_level = l:battery_level
redraw!
endif
call timer_start(30000, 'SetBatteryLevel')
endfunction
void render() {
renderTexture(arrowTexture, arrow->x, arrow->y, arrow->w, arrow->h, arrow->angle * 180 / PI, 255);
}
void update(float dt) {
// Update velocity
float vx = arrow->vx * arrow->time;
float vy = arrow->vy * arrow->time + 0.5 * world.gravity * arrow->time * arrow->time;
// Update position
arrow->x = arrow->startx + vx;
arrow->y = arrow->starty + vy;
void fireArrow(Arrow* arrow, Entity* archer, Entity* target) {
// Arrow starts from its archer
arrow->x = archer->x;
arrow->y = archer->y;
arrow->startx = arrow->x;
arrow->starty = arrow->y;
// Calculate distance between arrow and the target
float dx = target->x - entity->x;
struct Arrow {
float x, y; // x, y position coordinates
Sint32 w, h; // width & height
float vx, vy; // x, y velocity coordinates
float startx, starty; // x, y start coordinates
float angle; // current rotation angle
float time; // flight time (t = 0 -> launch arrow | t = FLIGHT_DURATION -> arrow arrives to target)
};
void update(float dt) {
// update velocity according to key pressed, collisions or other events
entity->vx += f(dt);
entity->vx += f(dt);
// update position according to the updated velocity
entity->x += entity->vx * dt;
entity->y += entity->vy * dt;
// (http://lazyfoo.net/tutorials/SDL/16_true_type_fonts/index.php)
bool renderScoreText(std::string textureText, SDL_Color textColor) {
// Free old texture
SDL_DestroyTexture(scoreTexture);
// Render text on a surface
SDL_Surface* textSurface = TTF_RenderText_Solid(gFont, textureText.c_str(), textColor);
if (textSurface == NULL) {
printf("Unable to render text surface! SDL_ttf Error: %s\n", TTF_GetError());
void update() {
// If survivor is dead..
if (survivor.state == "state_dead") {
// Check if "R" key is pressed and restart
const Uint8* keys = SDL_GetKeyboardState(NULL);
if (keys[SDL_SCANCODE_R]) {
restart();
}
// Restart game
void restart() {
survivor.x = 200;
survivor.y = 100;
survivor.vX = 0;
survivor.vY = 0;
survivor.scaleX = 1;
survivor.frameX = 0;
survivor.frameY = 0;
survivor.alive = true;
void loadMedia() {
// Init font
gFont = TTF_OpenFont("assets/3Dventure.ttf", 28);
if (gFont == NULL) {
printf("Failed to load font! SDL_ttf Error: %s\n", TTF_GetError());
}
// Load high score from file (http://lazyfoo.net/tutorials/SDL/33_file_reading_and_writing/index.php)
SDL_RWops* file = SDL_RWFromFile("score.bin", "r+b");