/build |
# Makefile generated by smake. | |||||
PROJNAME = cpplat | |||||
PROJTYPE = exe | |||||
SRCS = src/Body.cc src/main.cc src/Player.cc | |||||
HDRS = src/Body.h src/common.h src/Player.h | |||||
OBJS = $(patsubst src/%,$(BUILD)/obj/%.o,$(SRCS)) | |||||
DEPS = $(patsubst src/%,$(BUILD)/dep/%.d,$(SRCS)) | |||||
PUBLICHDRS = | |||||
# Defaults | |||||
PKGS = | |||||
EXTRADEPS = | |||||
EXTRAPUBLICDEPS = | |||||
CONFIG ?= release | |||||
BUILDDIR ?= build | |||||
BUILD ?= $(BUILDDIR)/$(CONFIG) | |||||
WARNINGS = -Wall -Wextra -Wno-unused-parameter | |||||
SMAKEFILE ?= Smakefile | |||||
DESTDIR ?= | |||||
PREFIX ?= /usr/local | |||||
PHONIES = dumpdeps dumppublicdeps dumpprojtype clean cleanall | |||||
CONFIGS = release sanitize debug | |||||
PKG_CONFIG ?= pkg-config | |||||
AR ?= ar | |||||
CCOPTS = -Iinclude | |||||
CCOPTS_release = -O2 -flto | |||||
CCOPTS_debug = -g | |||||
CCOPTS_sanitize = -fsanitize=address -fsanitize=undefined $(CCOPTS_debug) | |||||
LDOPTS = | |||||
LDOPTS_release = -flto | |||||
LDOPTS_debug = | |||||
LDOPTS_sanitize = -fsanitize=address -fsanitize=undefined $(LDOPTS_debug) | |||||
runpfx = @echo $(1) $(2) && $(2) | |||||
.PHONY: all | |||||
all: $(BUILD)/$(PROJNAME) | |||||
-include $(SMAKEFILE) | |||||
ifeq ($(filter $(CONFIGS),$(CONFIG)),) | |||||
ifeq ($(filter cleanall,$(MAKECMDGOALS)),) | |||||
$(error Unknown config '$(CONFIG)'. Supported configs: $(CONFIGS)) | |||||
endif | |||||
endif | |||||
ifneq ($(PKGS),) | |||||
CCOPTS += $(shell $(PKG_CONFIG) --cflags $(PKGS)) | |||||
LDOPTS += $(shell $(PKG_CONFIG) --libs $(PKGS)) | |||||
endif | |||||
CFLAGS := $(CCOPTS_$(CONFIG)) $(CCOPTS) $(WARNINGS) $(CFLAGS) | |||||
CXXFLAGS := $(CCOPTS_$(CONFIG)) $(CCOPTS) $(WARNINGS) $(CXXFLAGS) | |||||
LDFLAGS := $(LDOPTS_$(CONFIG)) $(LDOPTS) $(LDFLAGS) | |||||
$(BUILD)/$(PROJNAME): $(OBJS) | |||||
@mkdir -p $(@D) | |||||
$(call runpfx,'(LD)',$(CC) -o $@ $(OBJS) $(LDFLAGS)) | |||||
touch $(BUILD)/.built | |||||
@echo '(OK)' Created $@. | |||||
$(PROJNAME): $(BUILD)/$(PROJNAME) | |||||
cp $< $@ | |||||
@echo '(OK)' Created $@. | |||||
$(BUILD)/obj/%.c.o: src/%.c $(EXTRAPUBLICDEPS) | |||||
@mkdir -p $(@D) | |||||
$(call runpfx,'(CC)',$(CC) -o $@ -c $< $(CFLAGS)) | |||||
$(BUILD)/obj/%.cc.o: src/%.cc $(EXTRAPUBLICDEPS) | |||||
@mkdir -p $(@D) | |||||
$(call runpfx,'(CXX)',$(CXX) -o $@ -c $< $(CXXFLAGS)) | |||||
$(BUILD)/dep/%.c.d: src/%.c $(HDRS) | |||||
@mkdir -p $(@D) | |||||
$(call runpfx,'(DEP)',$(CC) -o $@ -MM $< -MT $(patsubst src/%,$(BUILD)/obj/%.o,$<) $(CFLAGS)) | |||||
$(BUILD)/dep/%.cc.d: src/%.cc $(HDRS) | |||||
@mkdir -p $(@D) | |||||
$(call runpfx,'(DEP)',$(CXX) -o $@ -MM $< -MT $(patsubst src/%,$(BUILD)/obj/%.o,$<) $(CXXFLAGS)) | |||||
.PHONY: install | |||||
install: $(BUILD)/$(PROJNAME) | |||||
mkdir -p $(DESTDIR)$(PREFIX)/bin | |||||
cp -f $^ $(DESTDIR)$(PREFIX)/bin | |||||
chmod 755 $(DESTDIR)$(PREFIX)/bin/$(PROJNAME) | |||||
.PHONY: uninstall | |||||
uninstall: | |||||
rm -f $(DESTDIR)$(PREFIX)/bin/$(PROJNAME) | |||||
.PHONY: clean | |||||
clean: | |||||
rm -rf $(PROJNAME) $(BUILD) | |||||
.PHONY: cleanall | |||||
cleanall: | |||||
rm -rf $(BUILDDIR) | |||||
.PHONY: dumpdeps | |||||
dumpdeps: | |||||
@echo $(addprefix $(PREPEND),$(SRCS) $(HDRS) $(EXTRADEPS)) | |||||
.PHONY: dumppublicdeps | |||||
dumppublicdeps: | |||||
@echo $(addprefix $(PREPEND),$(PUBLICHDRS) $(EXTRAPUBLICDEPS)) | |||||
.PHONY: dumpprojtype | |||||
dumpprojtype: | |||||
@echo $(PROJTYPE) | |||||
ifeq ($(filter $(PHONIES),$(MAKECMDGOALS)),) | |||||
-include $(DEPS) | |||||
endif |
PKGS = sfml-all | |||||
LDFLAGS += -lstdc++ |
#include "Body.h" | |||||
void Body::outline(Win &win) { | |||||
win.setPos(pos_); | |||||
sf::RectangleShape rect(size_); | |||||
rect.setFillColor(sf::Color::Transparent); | |||||
rect.setOutlineColor(sf::Color(128, 128, 128)); | |||||
rect.setOutlineThickness(2 / UNIT_SIZE); | |||||
win.draw(rect); | |||||
} | |||||
void Body::update(float dt) { | |||||
vel_ += force_ * dt; | |||||
pos_ += vel_ * dt; | |||||
force_ = { 0, 0 }; | |||||
} |
#pragma once | |||||
#include <SFML/Graphics.hpp> | |||||
#include "common.h" | |||||
class Body { | |||||
public: | |||||
Vec2 force_ = { 0, 0 }; | |||||
Vec2 vel_ = { 0, 0 }; | |||||
Vec2 pos_; | |||||
Vec2 size_; | |||||
Body(Vec2 pos, Vec2 size): | |||||
pos_(pos), size_(size) {}; | |||||
void outline(Win &win); | |||||
void update(float dt); | |||||
}; |
#include "Player.h" | |||||
void Player::draw(Win &win) { | |||||
body_.outline(win); | |||||
} | |||||
void Player::update(float dt) { | |||||
body_.vel_.x = 1; | |||||
body_.update(dt); | |||||
if (body_.pos_.x > 20) | |||||
body_.pos_.x = 0; | |||||
} |
#pragma once | |||||
#include <SFML/Graphics.hpp> | |||||
#include "common.h" | |||||
#include "Body.h" | |||||
class Player { | |||||
public: | |||||
Player(Vec2 pos): | |||||
body_(pos, Vec2(1, 1)) {} | |||||
void draw(Win &win); | |||||
void update(float dt); | |||||
private: | |||||
Body body_; | |||||
}; |
#pragma once | |||||
#include <SFML/System/Vector2.hpp> | |||||
#include <SFML/Graphics.hpp> | |||||
#define UNIT_SIZE 32.0 | |||||
class Vec2: public sf::Vector2<float> { | |||||
public: | |||||
using sf::Vector2<float>::Vector2; | |||||
Vec2 operator+(Vec2 &vec) { | |||||
return Vec2(x + vec.x, y + vec.y); | |||||
} | |||||
Vec2 &operator+=(Vec2 &vec) { | |||||
this->x += vec.x; | |||||
this->y += vec.y; | |||||
return *this; | |||||
} | |||||
Vec2 operator-(Vec2 &vec) { | |||||
return Vec2(x - vec.x, y - vec.y); | |||||
} | |||||
Vec2 &operator-=(Vec2 &vec) { | |||||
this->x -= vec.x; | |||||
this->y -= vec.y; | |||||
return *this; | |||||
} | |||||
Vec2 operator*(Vec2 &vec) { | |||||
return Vec2(x * vec.x, y * vec.y); | |||||
} | |||||
Vec2 &operator*=(Vec2 &vec) { | |||||
this->x *= vec.x; | |||||
this->y *= vec.y; | |||||
return *this; | |||||
} | |||||
Vec2 operator/(Vec2 &vec) { | |||||
return Vec2(x / vec.x, y / vec.y); | |||||
} | |||||
Vec2 &operator/=(Vec2 &vec) { | |||||
this->x /= vec.x; | |||||
this->y /= vec.y; | |||||
return *this; | |||||
} | |||||
}; | |||||
struct Win { | |||||
public: | |||||
sf::RenderWindow &window_; | |||||
sf::Transform &transform_; | |||||
Vec2 curr_pos_ = { 0, 0 }; | |||||
void setPos(Vec2 &pos) { | |||||
transform_.translate(-curr_pos_.x, -curr_pos_.y); | |||||
transform_.translate(pos.x, pos.y); | |||||
curr_pos_ = pos; | |||||
} | |||||
void draw(const sf::Drawable &drawable) { | |||||
window_.draw(drawable, transform_); | |||||
} | |||||
}; |
#include <vector> | |||||
#include <time.h> | |||||
#include "common.h" | |||||
#include "Player.h" | |||||
double getTime() { | |||||
struct timespec ts; | |||||
clock_gettime(CLOCK_MONOTONIC, &ts); | |||||
return (double)ts.tv_sec + (double)ts.tv_nsec / 1000000000.0; | |||||
} | |||||
template<typename T> | |||||
void draw_ents(std::vector<T> ents) { | |||||
for (auto &ent: ents) | |||||
ent.draw(); | |||||
} | |||||
int main() { | |||||
sf::RenderWindow window(sf::VideoMode(800, 600), "SFML works!"); | |||||
sf::Transform transform; | |||||
transform.scale(UNIT_SIZE, UNIT_SIZE); | |||||
Win win = { window, transform }; | |||||
Player player(Vec2(1, 1)); | |||||
double prevtime = getTime(); | |||||
double acc = 0; | |||||
int fcount = 0; | |||||
while (window.isOpen()) { | |||||
sf::Event event; | |||||
while (window.pollEvent(event)) { | |||||
if (event.type == sf::Event::Closed) | |||||
window.close(); | |||||
} | |||||
double now = getTime(); | |||||
double dt = now - prevtime; | |||||
prevtime = now; | |||||
acc += dt; | |||||
fcount += 1; | |||||
if (acc >= 1) { | |||||
fprintf(stderr, "fps %i\n", fcount); | |||||
acc = 0; | |||||
fcount = 0; | |||||
} | |||||
window.clear(sf::Color(135, 206, 250)); | |||||
player.draw(win); | |||||
player.update(dt); | |||||
window.display(); | |||||
} | |||||
return 0; | |||||
} |