Browse Source

actual game idea and stuff

opengl-renderer-broken
Martin Dørum 4 years ago
parent
commit
42804b32e7
13 changed files with 144 additions and 21 deletions
  1. 2
    2
      Makefile
  2. 1
    0
      Smakefile
  3. 22
    0
      idea.md
  4. 9
    1
      src/Body.cc
  5. 6
    2
      src/Body.h
  6. 20
    0
      src/Game.cc
  7. 18
    0
      src/Game.h
  8. 13
    4
      src/Player.cc
  9. 5
    1
      src/Player.h
  10. 10
    0
      src/WorldPlane.cc
  11. 10
    0
      src/WorldPlane.h
  12. 1
    0
      src/common.h
  13. 27
    11
      src/main.cc

+ 2
- 2
Makefile View File

@@ -3,8 +3,8 @@
PROJNAME = cpplat
PROJTYPE = exe

SRCS = src/Body.cc src/main.cc src/Player.cc
HDRS = src/Body.h src/common.h src/Player.h
SRCS = src/Body.cc src/Game.cc src/main.cc src/Player.cc src/WorldPlane.cc
HDRS = src/Body.h src/common.h src/Game.h src/Player.h src/WorldPlane.h
OBJS = $(patsubst src/%,$(BUILD)/obj/%.o,$(SRCS))
DEPS = $(patsubst src/%,$(BUILD)/dep/%.d,$(SRCS))
PUBLICHDRS =

+ 1
- 0
Smakefile View File

@@ -1,2 +1,3 @@
PROJNAME = cpplat
PKGS = sfml-all
LDFLAGS += -lstdc++

+ 22
- 0
idea.md View File

@@ -0,0 +1,22 @@
# Game Idea

* 2D tile-based platformer-esque game
* Circuit network shit
* Move along a third axis, the Z axis
* "Dimentional Shifter"
* Mobs ofc
* Some kind of crafting? IDK
* Progression?
* Minecraft-like exploration-driven progression with no clear path?
* Terraria-like linear-ish boss-driven progression?
* Power system
* Circuit network shit requires power
* Maybe emulate a simplified electrical system?
* Transistors could be the core, where power flows from A to B if C is
receiving power (B = C ? A + C : 0)
* Everything (pistons, doors, gates, sensors, whatever) uses power
* Power gen could be one of the main "goals" of the game
* Turing machines?
* Idk, had this idea for a minecraft mod once.
* A robot which runs a dialect of Brainfuck with some extra operations for
I/O to interact with the world.

+ 9
- 1
src/Body.cc View File

@@ -1,5 +1,13 @@
#include "Body.h"

void Body::friction(float coef) {
force_ += -vel_ * coef;
}

void Body::gravity(Vec2 g) {
force_ += g * mass_;
}

void Body::outline(Win &win) {
win.setPos(pos_);

@@ -11,7 +19,7 @@ void Body::outline(Win &win) {
}

void Body::update(float dt) {
vel_ += force_ * dt;
vel_ += (force_ / mass_) * dt;
pos_ += vel_ * dt;
force_ = { 0, 0 };
}

+ 6
- 2
src/Body.h View File

@@ -10,9 +10,13 @@ public:
Vec2 vel_ = { 0, 0 };
Vec2 pos_;
Vec2 size_;
float mass_;

Body(Vec2 pos, Vec2 size):
pos_(pos), size_(size) {};
Body(Vec2 pos, Vec2 size, float mass):
pos_(pos), size_(size), mass_(mass) {};

void friction(float coef);
void gravity(Vec2 g = Vec2(0, 9.81));

void outline(Win &win);
void update(float dt);

+ 20
- 0
src/Game.cc View File

@@ -0,0 +1,20 @@
#include "Game.h"

void Game::draw(Win &win) {
for (WorldPlane *plane: planes_)
plane->draw(win);

player_->draw(win);
}

void Game::update(float dt) {
for (WorldPlane *plane: planes_)
plane->update(dt);

player_->update(dt);
}

void Game::tick() {
for (WorldPlane *plane: planes_)
plane->tick();
}

+ 18
- 0
src/Game.h View File

@@ -0,0 +1,18 @@
#pragma once

#include <vector>

#include "common.h"
#include "WorldPlane.h"
#include "Player.h"

class Game {
public:
Player *player_;
WorldPlane *current_plane_;
std::vector<WorldPlane *> planes_;

void draw(Win &win);
void update(float dt);
void tick();
};

+ 13
- 4
src/Player.cc View File

@@ -1,13 +1,22 @@
#include "Player.h"

using Keyboard = sf::Keyboard;

void Player::draw(Win &win) {
body_.outline(win);
}

void Player::update(float dt) {
body_.vel_.x = 1;
body_.update(dt);
if (Keyboard::isKeyPressed(Keyboard::W) || Keyboard::isKeyPressed(Keyboard::Up))
body_.force_.y -= force;
if (Keyboard::isKeyPressed(Keyboard::S) || Keyboard::isKeyPressed(Keyboard::Down))
body_.force_.y += force;
if (Keyboard::isKeyPressed(Keyboard::A) || Keyboard::isKeyPressed(Keyboard::Left))
body_.force_.x -= force;
if (Keyboard::isKeyPressed(Keyboard::D) || Keyboard::isKeyPressed(Keyboard::Right))
body_.force_.x += force;

if (body_.pos_.x > 20)
body_.pos_.x = 0;
body_.friction(friction);
body_.gravity();
body_.update(dt);
}

+ 5
- 1
src/Player.h View File

@@ -8,11 +8,15 @@
class Player {
public:
Player(Vec2 pos):
body_(pos, Vec2(1, 1)) {}
body_(pos, Vec2(1, 1), mass) {}

void draw(Win &win);
void update(float dt);

private:
static constexpr float force = 600;
static constexpr float friction = 100;
static constexpr float mass = 50;

Body body_;
};

+ 10
- 0
src/WorldPlane.cc View File

@@ -0,0 +1,10 @@
#include "WorldPlane.h"

void WorldPlane::draw(Win &win) {
}

void WorldPlane::update(float dt) {
}

void tick() {
}

+ 10
- 0
src/WorldPlane.h View File

@@ -0,0 +1,10 @@
#pragma once

#include "common.h"

class WorldPlane {
public:
void draw(Win &win);
void update(float dt);
void tick();
};

+ 1
- 0
src/common.h View File

@@ -4,6 +4,7 @@
#include <SFML/Graphics.hpp>

#define UNIT_SIZE 32.0
#define TICK_RATE 20

class Vec2: public sf::Vector2<float> {
public:

+ 27
- 11
src/main.cc View File

@@ -1,8 +1,10 @@
#include <vector>
#include <time.h>
#include <unistd.h>

#include "common.h"
#include "Player.h"
#include "Game.h"

double getTime() {
struct timespec ts;
@@ -17,41 +19,55 @@ void draw_ents(std::vector<T> ents) {
}

int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!");
sf::RenderWindow window(sf::VideoMode(800, 600), "good gaem");
window.setVerticalSyncEnabled(true);
sf::Transform transform;
transform.scale(UNIT_SIZE, UNIT_SIZE);

Win win = { window, transform };

Player player(Vec2(1, 1));
Game game;
game.player_ = new Player(Vec2(1, 1));

double prevtime = getTime();
double acc = 0;
double fpsAcc = 0;
double tickAcc = 0;
int fcount = 0;

while (window.isOpen()) {
sf::Event event;
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed)
if (event.type == sf::Event::Closed) {
window.close();
} else if (event.type == sf::Event::Resized) {
sf::FloatRect visibleArea(0, 0, event.size.width, event.size.height);
window.setView(sf::View(visibleArea));
}
}

// Display FPS
double now = getTime();
double dt = now - prevtime;
prevtime = now;
acc += dt;
fpsAcc += dt;
fcount += 1;
if (acc >= 1) {
fprintf(stderr, "fps %i\n", fcount);
acc = 0;
if (fpsAcc >= 1) {
fprintf(stderr, "FPS: %i\n", fcount);
fpsAcc -= 1;
fcount = 0;
}

window.clear(sf::Color(135, 206, 250));
game.update(dt);

player.draw(win);
player.update(dt);
// Call tick TICK_RATE times per second
tickAcc += dt;
while (tickAcc >= 1.0 / TICK_RATE) {
tickAcc -= 1.0 / TICK_RATE;
game.tick();
}

window.clear(sf::Color(135, 206, 250));
game.draw(win);
window.display();
}


Loading…
Cancel
Save