summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2016-12-30 21:04:57 +0100
committertomsmeding <tom.smeding@gmail.com>2016-12-30 21:04:57 +0100
commit9f84ef2686feab6c82f7f523319fda65ff077962 (patch)
treeb625f31ba5666694caa7ec77c75965c256f4519d
parent6c0a0cc29b00d0a58a3ee55a8a7e4909ebf3454c (diff)
Have draw_text working with SFML
-rw-r--r--Makefile4
-rw-r--r--global.h1
-rw-r--r--library.cpp19
-rw-r--r--main.cpp57
-rw-r--r--main.h13
-rw-r--r--object_base.cpp4
-rw-r--r--sprite.cpp2
-rw-r--r--sprite.h4
8 files changed, 78 insertions, 26 deletions
diff --git a/Makefile b/Makefile
index 54d6f53..2e9ed50 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
CXX := g++
-CXXFLAGS := -Wall -Wextra -std=c++11 -O2 -I.
-LIBS := -L/usr/local/lib -lfltk -lfltk_images
+CXXFLAGS := -Wall -Wextra -std=c++11 -O2 -I. -I/usr/local/lib
+LIBS := -L/usr/local/lib -lsfml-{system,window,graphics}
BIN := game
object_src := $(wildcard src/objects/*.cpp)
diff --git a/global.h b/global.h
index e980582..44e520a 100644
--- a/global.h
+++ b/global.h
@@ -10,6 +10,5 @@ using namespace std;
class Global{
public:
- int room_speed=30;
vector<shared_ptr<Object>> objects;
};
diff --git a/library.cpp b/library.cpp
index cee789e..eda5628 100644
--- a/library.cpp
+++ b/library.cpp
@@ -2,15 +2,30 @@
#include <iostream>
#include <cstdarg>
#include <cassert>
+#include <SFML/Graphics.hpp>
#include "global.h"
#include "library.h"
-#include "main.h"
using namespace std;
+extern sf::RenderWindow window;
+
static void draw_text(int x,int y,const char *s,size_t len){
- fl_draw(s,len,x,y);
+ static sf::Font font;
+ static bool fontLoaded=false;
+ static sf::Text text;
+
+ if(!fontLoaded){
+ font.loadFromFile("/Library/Fonts/Arial.ttf");
+ fontLoaded=true;
+ }
+
+ text.setFont(font);
+ text.setFillColor(sf::Color::Black);
+ text.setString(string(s,len));
+ text.setPosition(x,y);
+ window.draw(text);
}
void draw_text(int x,int y,const char *s){
diff --git a/main.cpp b/main.cpp
index b1e394e..a10231e 100644
--- a/main.cpp
+++ b/main.cpp
@@ -3,7 +3,7 @@
#include <cstring>
#include <cstdlib>
#include <sys/time.h>
-#include "main.h"
+#include <SFML/Graphics.hpp>
#include "object_base.h"
#include "global.h"
#include "library.h"
@@ -24,7 +24,58 @@ OBJECT_CLASSES_XLIST
Global global;
-static Fl_Window_draw *window;
+
+sf::RenderWindow window;
+
+
+void eventCycle(){
+ sf::Event event;
+ while(window.pollEvent(event)){
+ if(event.type==sf::Event::Closed){
+ window.close();
+ return;
+ } else if(event.type==sf::Event::KeyPressed){
+ if(event.key.code==sf::Keyboard::Escape){
+ window.close();
+ return;
+ }
+ }
+ }
+
+ for(int i=0;i<(int)global.objects.size();i++){
+ global.objects[i]->step();
+ }
+
+ window.clear(sf::Color::White);
+
+ for(int i=0;i<(int)global.objects.size();i++){
+ global.objects[i]->draw();
+ }
+
+ window.display();
+}
+
+int main(void){
+ struct timeval tv;
+ gettimeofday(&tv,nullptr);
+ srand(tv.tv_sec*1000000+tv.tv_usec);
+
+ window.create(sf::VideoMode(640,480),GAME_NAME);
+ window.setVerticalSyncEnabled(true);
+
+ global.objects.push_back(makeobjectbyname(0,0,"obj_control"));
+ global.objects[0]->create();
+
+ while(window.isOpen()){
+ eventCycle();
+ }
+
+ for(shared_ptr<Object> p : global.objects){
+ p->destroy();
+ }
+}
+
+/*static Fl_Window_draw *window;
@@ -70,4 +121,4 @@ int main(int argc,char **argv){
p->destroy();
}
return ret;
-}
+}*/
diff --git a/main.h b/main.h
deleted file mode 100644
index 218d5d2..0000000
--- a/main.h
+++ /dev/null
@@ -1,13 +0,0 @@
-#pragma once
-
-#include <FL/Fl.H>
-#include <FL/Fl_Window.H>
-#include <FL/Fl_draw.H>
-
-
-class Fl_Window_draw : public Fl_Window{
-public:
- Fl_Window_draw(int w,int h,const char *title);
- Fl_Window_draw(int x,int y,int w,int h,const char *label);
- void draw(void);
-};
diff --git a/object_base.cpp b/object_base.cpp
index 18d7e3a..ce97f2a 100644
--- a/object_base.cpp
+++ b/object_base.cpp
@@ -21,12 +21,12 @@ void Object::destroy(void){}
void Object::step(void){}
void Object::draw(void){
- if(!sprite_index){
+ /*if(!sprite_index){
return;
}
if(!sprite_index->fl_image){
sprite_index->fl_image=new Fl_PNG_Image(nullptr,sprite_index->image,sprite_index->image_len);
assert(!sprite_index->fl_image->fail());
}
- sprite_index->fl_image->draw((int)x,(int)y);
+ sprite_index->fl_image->draw((int)x,(int)y);*/
}
diff --git a/sprite.cpp b/sprite.cpp
index d751230..e3503ea 100644
--- a/sprite.cpp
+++ b/sprite.cpp
@@ -2,4 +2,4 @@
Sprite::Sprite(const unsigned char *image,unsigned int image_len)
- :image(image),image_len(image_len),fl_image(nullptr){}
+ :image(image),image_len(image_len)/*,fl_image(nullptr)*/{}
diff --git a/sprite.h b/sprite.h
index db3e276..b52026a 100644
--- a/sprite.h
+++ b/sprite.h
@@ -1,13 +1,13 @@
#pragma once
-#include <FL/Fl_PNG_Image.H>
+//#include <FL/Fl_PNG_Image.H>
class Sprite{
public:
const unsigned char *image;
unsigned int image_len;
- Fl_PNG_Image *fl_image;
+ //Fl_PNG_Image *fl_image;
Sprite(const unsigned char *image,unsigned int image_len);
};