Skip to content

Instantly share code, notes, and snippets.

View Mikepicker's full-sized avatar

Michele Rullo Mikepicker

  • Anzio, RM
View GitHub Profile

Keybase proof

I hereby claim:

  • I am mikepicker on github.
  • I am mikepicker (https://keybase.io/mikepicker) on keybase.
  • I have a public key ASBV-rCdor5LkYkzuyARoo81AnHDMm-8wLz0ocYzRGSiNQo

To claim this, I am signing this object:

@Mikepicker
Mikepicker / 1-hold-data.cpp
Last active November 16, 2017 13:53
#1 - hold data
// Game objects
struct Background {
SDL_Texture* texture;
};
struct Platform {
int x, y, w, h;
SDL_Texture* texture;
};
@Mikepicker
Mikepicker / 1-load.cpp
Last active November 16, 2017 13:52
#1 - load
bool loadMedia() {
// Loading success flag
bool success = true;
// Init background
background.texture = loadTexture("assets/background.png");
SDL_QueryTexture(background.texture, NULL, NULL, &background.w, &background.h);
background.x = 0;
background.y = SCREEN_HEIGHT-background.h;
@Mikepicker
Mikepicker / render.cpp
Created November 16, 2017 13:55
#1 - render
void render() {
// Render background
SDL_Rect dstBackground = { .x = background.x, .y = background.y, .w = background.w, .h = background.h };
SDL_RenderCopy(gRenderer, background.texture, NULL, &dstBackground);
// Render platform
SDL_Rect dstPlatf = { .x = platform.x, .y = platform.y, .w = platform.w, .h = platform.h };
SDL_RenderCopy(gRenderer, platform.texture, NULL, &dstPlatf);
}
@Mikepicker
Mikepicker / 2-player.cpp
Last active November 16, 2017 18:16
#2 - player
struct Survivor {
int x, y, w, h;
int scaleX, scaleY;
int frameX, frameY;
int animSpeed = 6;
int speed = 3;
bool animCompleted = false;
bool shot = false;
std::string state;
SDL_Texture* texture;
@Mikepicker
Mikepicker / 2-load-player.cpp
Created November 16, 2017 18:15
#2 - player
bool loadMedia() {
// Init survivor
survivor.texture = loadTexture("assets/survivor.png");
survivor.x = 200;
survivor.y = 100;
survivor.w = 64;
survivor.h = 64;
survivor.scaleX = 1;
survivor.scaleY = 1;
@Mikepicker
Mikepicker / 2-update-player.cpp
Created November 16, 2017 18:17
#2 - player
void update() {
// Physics
survivor.y += world.gravity;
// Platform collision
if (survivor.y + survivor.h > platform.y &&
survivor.x + 48 >= platform.x &&
survivor.x + survivor.w - 48 <= platform.x + platform.w &&
survivor.y + (survivor.h/2) < platform.y) {
void render() {
// Clear screen
SDL_RenderClear(gRenderer);
// Render survivor
SDL_Rect srcSurv = { .x = (survivor.frameX / survivor.animSpeed) * survivor.w, .y = survivor.frameY * survivor.h, .w = survivor.w, .h = survivor.h };
SDL_Rect dstSurv = { .x = survivor.x, .y = survivor.y, .w = survivor.w, .h = survivor.h };
SDL_RendererFlip flip = survivor.scaleX == 1 ? SDL_FLIP_NONE : SDL_FLIP_HORIZONTAL;
SDL_RenderCopyEx(gRenderer, survivor.texture, &srcSurv, &dstSurv, 0, NULL, flip);
const int BULLET_COUNT = 10;
SDL_Texture* bulletTexture;
int bulletWidth, bulletHeight;
int bulletSpeed = 20;
struct Bullet {
int x, y;
int dir = 1;
bool alive = false;
};
bool loadMedia() {
// Init bullet
bulletTexture = loadTexture("assets/bullet.png");
SDL_QueryTexture(bulletTexture, NULL, NULL, &bulletWidth, &bulletHeight);
}