Skip to content

Instantly share code, notes, and snippets.

@jdiez17
Last active August 29, 2015 13:56
Show Gist options
  • Save jdiez17/9306977 to your computer and use it in GitHub Desktop.
Save jdiez17/9306977 to your computer and use it in GitHub Desktop.
#ifndef OBJECTSYSTEM_HPP
#define OBJECTSYSTEM_HPP
#include "gameobject.hpp"
#include "particle.hpp"
#include <vector>
class Game: public GameObject {
std::vector<GameObject*> objects;
public:
Game();
~Game();
virtual void Update(sf::Time dt);
virtual void MouseUpdate(sf::Vector2f position);
virtual void draw(sf::RenderTarget& target, sf::RenderStates states) const;
virtual void* dumpState();
};
typedef Game* (*game_create_f)();
typedef void (*game_delete_f)(Game*);
#endif
#include <SFML/Graphics/RenderWindow.hpp>
#include <SFML/System/Thread.hpp>
#include <SFML/System/Clock.hpp>
#include <SFML/Window/Event.hpp>
#include <errno.h>
#include <dlfcn.h>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <sys/inotify.h>
#include <iostream>
#include "game.hpp"
#define EVENT_SIZE sizeof(struct inotify_event)
#define BUF_LEN 1024 * (EVENT_SIZE + 16)
void* handle;
game_delete_f game_delete;
Game* create_game() {
if(handle) {
dlclose(handle);
}
handle = dlopen("./libgame.so", RTLD_LAZY);
game_delete = (game_delete_f) dlsym(handle, "game_delete");
return ((game_create_f) dlsym(handle, "game_create")) ();
}
void wait_until_change() {
char buffer[BUF_LEN];
int fd = inotify_init();
int wd = inotify_add_watch(fd, "./libgame.so", IN_ATTRIB);
read(fd, buffer, BUF_LEN); // we don't actually care about the info
inotify_rm_watch(fd, wd);
close(fd);
}
void monitor_thread(bool* time_to_reload) {
while(1) {
wait_until_change();
sleep(1);
*time_to_reload = true;
}
}
int main() {
bool time_to_reload = false;
Game* game = create_game();
sf::RenderWindow window(sf::VideoMode(200, 200), "Particles");
sf::Clock clock;
window.setFramerateLimit(60);
window.setVerticalSyncEnabled(true);
sf::Thread monitor(&monitor_thread, &time_to_reload);
monitor.launch();
while(window.isOpen()) {
sf::Event ev;
while(window.pollEvent(ev)) {
if(ev.type == sf::Event::Closed) {
window.close();
}
if(ev.type == sf::Event::KeyPressed) {
time_to_reload = true;
}
}
if(time_to_reload) {
time_to_reload = false;
game_delete(game);
game = create_game();
}
game->MouseUpdate(window.mapPixelToCoords(sf::Mouse::getPosition(window)));
sf::Time dt = clock.restart();
game->Update(dt);
window.clear();
window.draw(*game);
window.display();
}
return 0;
}
CPP_FILES := $(wildcard src/*.cpp)
OBJ_FILES := $(addprefix obj/,$(notdir $(CPP_FILES:.cpp=.o)))
LD_FLAGS := -lsfml-graphics -lsfml-window -lsfml-system -ldl
CC_FLAGS := -g -fPIC -std=c++11
#bin/main: $(OBJ_FILES)
# g++ $(LD_FLAGS) -o $@ $^
bin/main: libgame.so $(OBJ_FILES)
g++ $(LD_FLAGS) -o $@ obj/main.o
libgame.so: obj/game.o obj/particle.o
g++ -fPIC -shared $^ -o $@
obj/%.o: src/%.cpp
g++ $(CC_FLAGS) -c -o $@ $<
clean:
rm -f bin/main
rm -f obj/*
run: bin/main
./bin/main
debug: bin/main
gdb ./bin/main
.PHONY: clean debug
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment