summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortomsmeding <tom.smeding@gmail.com>2016-12-30 22:53:42 +0100
committertomsmeding <tom.smeding@gmail.com>2016-12-30 22:53:42 +0100
commit1002b8640b80c0ec4ba5c242ca2873ee1fc591d5 (patch)
tree66a34e66c3a078d9a7adc4593fa057dc0d8334dc
parentda86e33dcba7d66e2e5b55efa8a7265c5d2989b9 (diff)
Drawing text (also added Liberation font with license)
-rw-r--r--Makefile39
-rw-r--r--font.cpp8
-rw-r--r--font.h13
-rw-r--r--library.cpp33
-rw-r--r--library.h3
-rw-r--r--object_wrapper.cpp1
-rw-r--r--src/fonts/LICENSE105
-rw-r--r--src/fonts/fnt_liber.ttfbin0 -> 350200 bytes
-rw-r--r--src/objects/obj_control.cpp1
9 files changed, 183 insertions, 20 deletions
diff --git a/Makefile b/Makefile
index a5f5962..2d77ef1 100644
--- a/Makefile
+++ b/Makefile
@@ -1,5 +1,5 @@
CXX := g++
-CXXFLAGS := -Wall -Wextra -std=c++11 -O2 -I. -I/usr/local/lib
+CXXFLAGS := -Wall -Wextra -std=c++11 -O2 -fwrapv -I. -I/usr/local/lib
LIBS := -L/usr/local/lib -lsfml-{system,window,graphics}
BIN := game
@@ -19,8 +19,14 @@ sprite_skipped := $(filter-out %.png,$(sprite_allfiles))
sprite_obj := $(patsubst src/sprites/%.png,build/sprites/%.o,$(sprite_fnames))
sprite_externs := $(shell echo $$(for f in $(sprite_obj); do basename $$f | sed 's/\.[^.]*$$//' | sed 's/.*/extern Sprite *&;/'; done))
+font_allfiles := $(wildcard src/fonts/*)
+font_fnames := $(filter %.ttf,$(font_allfiles))
+font_skipped := $(filter-out %.ttf %/LICENSE,$(font_allfiles))
+font_obj := $(patsubst src/fonts/%.ttf,build/fonts/%.o,$(font_fnames))
+font_externs := $(shell echo $$(for f in $(font_obj); do basename $$f | sed 's/\.[^.]*$$//' | sed 's/.*/extern Font *&;/'; done))
+
base_src := $(wildcard *_base.cpp)
-base_src += library.cpp sprite.cpp main.cpp
+base_src += library.cpp sprite.cpp font.cpp main.cpp
base_obj := $(patsubst %.cpp,build/%.o,$(base_src))
.PHONY: all clean remake run
@@ -43,16 +49,16 @@ run: all
./$(BIN)
-$(BIN): $(base_obj) $(object_obj) $(sprite_obj)
+$(BIN): $(base_obj) $(object_obj) $(sprite_obj) $(font_obj)
echo Linking
$(CXX) $(CXXFLAGS) $(LIBS) -o $(BIN) $^
-build/main.o: main.cpp build/objects.h build/sprites.h
+build/main.o: main.cpp build/objects.h build/sprites.h build/fonts.h
echo Building main
mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) -c -o "$@" "$<" "-DOBJECT_CLASSES_XLIST=$(object_xlist)" "-DGAME_NAME=\"$(BIN)\""
-build/objects/%.o: build/objects/%.cpp build/objects.h build/sprites.h
+build/objects/%.o: build/objects/%.cpp build/objects.h build/sprites.h build/fonts.h
echo Building $*
mkdir -p $(dir $@)
$(CXX) $(CXXFLAGS) -c -o "$@" "$<"
@@ -80,15 +86,32 @@ build/%.o: %.cpp
build/sprites/%.o: build/sprites/%.cpp
echo Building sprite $*
mkdir -p $(dir $@)
- $(CXX) $(CXXFLAGS) -Wno-unused-const-variable -c -o "$@" "$<"
+ $(CXX) $(CXXFLAGS) -c -o "$@" "$<"
build/sprites/%.cpp: src/sprites/%.png
echo Generating for sprite $*
mkdir -p $(dir $@)
- printf "#include \"sprite.h\"\n\nstatic const unsigned char data[]={\n%s, 0\n};\n\nstatic Sprite struct_data=Sprite(data,%d);\nSprite *$*=&struct_data;\n" "$$($(BIN2C) <"$<")" $$(stat -f '%z' "$<") >"$@"
+ printf "#include \"sprite.h\"\n\nstatic const unsigned char data[]={\n%s\n};\n\nstatic Sprite struct_data=Sprite(data,%d);\nSprite *$*=&struct_data;\n" "$$($(BIN2C) <"$<")" $$(stat -f '%z' "$<") >"$@"
-build/sprites.h:
+build/sprites.h: $(sprite_fnames)
echo Collecting generated sprite files
mkdir -p $(dir $@)
printf "#include \"sprite.h\"\n\n$(sprite_externs)\n" >"$@"
test -z "$(sprite_skipped)" || printf "\x1B[1mWARNING: skipped files in src/sprites: $(notdir $(sprite_skipped))\x1B[0m\n"
+
+
+build/fonts/%.o: build/fonts/%.cpp
+ echo Building font $*
+ mkdir -p $(dir $@)
+ $(CXX) $(CXXFLAGS) -c -o "$@" "$<"
+
+build/fonts/%.cpp: src/fonts/%.*
+ echo Generating for font $*
+ mkdir -p $(dir $@)
+ printf "#include \"font.h\"\n\nstatic const unsigned char data[]={\n%s\n};\n\nstatic Font struct_data=Font(data,%d);\nFont *$*=&struct_data;\n" "$$($(BIN2C) <"$<")" $$(stat -f '%z' "$<") >"$@"
+
+build/fonts.h: $(font_fnames)
+ echo Collecting generated font files
+ mkdir -p $(dir $@)
+ printf "#include \"font.h\"\n\n$(font_externs)\n" >"$@"
+ test -z "$(font_skipped)" || printf "\x1B[1mWARNING: skipped files in src/fonts: $(notdir $(font_skipped))\x1B[0m\n"
diff --git a/font.cpp b/font.cpp
new file mode 100644
index 0000000..22020c9
--- /dev/null
+++ b/font.cpp
@@ -0,0 +1,8 @@
+#include <cassert>
+#include "font.h"
+
+
+Font::Font(const unsigned char *filedata,unsigned int file_len)
+ :filedata(filedata),file_len(file_len){
+ assert(sf_font.loadFromMemory(filedata,file_len));
+}
diff --git a/font.h b/font.h
new file mode 100644
index 0000000..059beb5
--- /dev/null
+++ b/font.h
@@ -0,0 +1,13 @@
+#pragma once
+
+#include <SFML/Graphics.hpp>
+
+
+class Font{
+public:
+ const unsigned char *filedata;
+ unsigned int file_len;
+ sf::Font sf_font;
+
+ Font(const unsigned char *filedata,unsigned int file_len);
+};
diff --git a/library.cpp b/library.cpp
index eda5628..12a9077 100644
--- a/library.cpp
+++ b/library.cpp
@@ -11,21 +11,25 @@ using namespace std;
extern sf::RenderWindow window;
-static void draw_text(int x,int y,const char *s,size_t len){
- static sf::Font font;
- static bool fontLoaded=false;
- static sf::Text text;
- if(!fontLoaded){
- font.loadFromFile("/Library/Fonts/Arial.ttf");
- fontLoaded=true;
+static const Font *currentFont=nullptr;
+static sf::Text sharedSfText;
+
+class Init{
+public:
+ Init(){
+ sharedSfText.setFillColor(sf::Color::Black);
+ sharedSfText.setCharacterSize(14);
}
+} init_object;
- text.setFont(font);
- text.setFillColor(sf::Color::Black);
- text.setString(string(s,len));
- text.setPosition(x,y);
- window.draw(text);
+static void draw_text(int x,int y,const char *s,size_t len){
+ if(currentFont!=nullptr){
+ sharedSfText.setFont(currentFont->sf_font);
+ }
+ sharedSfText.setString(string(s,len));
+ sharedSfText.setPosition(x,y);
+ window.draw(sharedSfText);
}
void draw_text(int x,int y,const char *s){
@@ -45,6 +49,11 @@ void draw_textf(int x,int y,const char *format,...){
}
+void draw_set_font(const Font *font){
+ currentFont=font;
+}
+
+
static void log(const char *buf,size_t len){
cerr<<"[LOG] ";
cerr.write(buf,len);
diff --git a/library.h b/library.h
index d078861..af2eedc 100644
--- a/library.h
+++ b/library.h
@@ -1,6 +1,7 @@
#pragma once
#include <memory>
+#include "font.h"
#include "global.h"
#include "object_base.h"
@@ -21,5 +22,7 @@ shared_ptr<Object> instance_create(int x,int y){
void draw_text(int x,int y,const char *s);
void draw_textf(int x,int y,const char *format,...) __attribute__((format (printf, 3, 4)));
+void draw_set_font(const Font *font);
+
void log(const char *s);
void logf(const char *format,...) __attribute__((format (printf, 1, 2)));
diff --git a/object_wrapper.cpp b/object_wrapper.cpp
index b539dd2..96002a6 100644
--- a/object_wrapper.cpp
+++ b/object_wrapper.cpp
@@ -6,6 +6,7 @@
OUT #include "library.h"
OUT #include "build/objects.h"
OUT #include "build/sprites.h"
+OUT #include "build/fonts.h"
OUT #include "global.h"
extern Global global;
diff --git a/src/fonts/LICENSE b/src/fonts/LICENSE
new file mode 100644
index 0000000..6f29750
--- /dev/null
+++ b/src/fonts/LICENSE
@@ -0,0 +1,105 @@
+Liberation font license:
+
+
+Digitized data copyright (c) 2010 Google Corporation
+ with Reserved Font Arimo, Tinos and Cousine.
+Copyright (c) 2012 Red Hat, Inc.
+ with Reserved Font Name Liberation.
+
+This Font Software is licensed under the SIL Open Font License,
+Version 1.1.
+
+This license is copied below, and is also available with a FAQ at:
+http://scripts.sil.org/OFL
+
+SIL OPEN FONT LICENSE Version 1.1 - 26 February 2007
+
+PREAMBLE The goals of the Open Font License (OFL) are to stimulate
+worldwide development of collaborative font projects, to support the font
+creation efforts of academic and linguistic communities, and to provide
+a free and open framework in which fonts may be shared and improved in
+partnership with others.
+
+The OFL allows the licensed fonts to be used, studied, modified and
+redistributed freely as long as they are not sold by themselves.
+The fonts, including any derivative works, can be bundled, embedded,
+redistributed and/or sold with any software provided that any reserved
+names are not used by derivative works. The fonts and derivatives,
+however, cannot be released under any other type of license. The
+requirement for fonts to remain under this license does not apply to
+any document created using the fonts or their derivatives.
+
+
+
+DEFINITIONS
+"Font Software" refers to the set of files released by the Copyright
+Holder(s) under this license and clearly marked as such.
+This may include source files, build scripts and documentation.
+
+"Reserved Font Name" refers to any names specified as such after the
+copyright statement(s).
+
+"Original Version" refers to the collection of Font Software components
+as distributed by the Copyright Holder(s).
+
+"Modified Version" refers to any derivative made by adding to, deleting,
+or substituting ? in part or in whole ?
+any of the components of the Original Version, by changing formats or
+by porting the Font Software to a new environment.
+
+"Author" refers to any designer, engineer, programmer, technical writer
+or other person who contributed to the Font Software.
+
+
+PERMISSION & CONDITIONS
+
+Permission is hereby granted, free of charge, to any person obtaining a
+copy of the Font Software, to use, study, copy, merge, embed, modify,
+redistribute, and sell modified and unmodified copies of the Font
+Software, subject to the following conditions:
+
+1) Neither the Font Software nor any of its individual components,in
+ Original or Modified Versions, may be sold by itself.
+
+2) Original or Modified Versions of the Font Software may be bundled,
+ redistributed and/or sold with any software, provided that each copy
+ contains the above copyright notice and this license. These can be
+ included either as stand-alone text files, human-readable headers or
+ in the appropriate machine-readable metadata fields within text or
+ binary files as long as those fields can be easily viewed by the user.
+
+3) No Modified Version of the Font Software may use the Reserved Font
+ Name(s) unless explicit written permission is granted by the
+ corresponding Copyright Holder. This restriction only applies to the
+ primary font name as presented to the users.
+
+4) The name(s) of the Copyright Holder(s) or the Author(s) of the Font
+ Software shall not be used to promote, endorse or advertise any
+ Modified Version, except to acknowledge the contribution(s) of the
+ Copyright Holder(s) and the Author(s) or with their explicit written
+ permission.
+
+5) The Font Software, modified or unmodified, in part or in whole, must
+ be distributed entirely under this license, and must not be distributed
+ under any other license. The requirement for fonts to remain under
+ this license does not apply to any document created using the Font
+ Software.
+
+
+
+TERMINATION
+This license becomes null and void if any of the above conditions are not met.
+
+
+
+DISCLAIMER
+THE FONT SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO ANY WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT
+OF COPYRIGHT, PATENT, TRADEMARK, OR OTHER RIGHT. IN NO EVENT SHALL THE
+COPYRIGHT HOLDER BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
+INCLUDING ANY GENERAL, SPECIAL, INDIRECT, INCIDENTAL, OR CONSEQUENTIAL
+DAMAGES, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+FROM, OUT OF THE USE OR INABILITY TO USE THE FONT SOFTWARE OR FROM OTHER
+DEALINGS IN THE FONT SOFTWARE.
+
diff --git a/src/fonts/fnt_liber.ttf b/src/fonts/fnt_liber.ttf
new file mode 100644
index 0000000..626dd93
--- /dev/null
+++ b/src/fonts/fnt_liber.ttf
Binary files differ
diff --git a/src/objects/obj_control.cpp b/src/objects/obj_control.cpp
index 6b6ab2c..2e63331 100644
--- a/src/objects/obj_control.cpp
+++ b/src/objects/obj_control.cpp
@@ -4,6 +4,7 @@ OUT #include <cstdlib>
double yd=0;
EVENT(create){
+ draw_set_font(fnt_liber);
x=10;
y=240;
log("Control object wow!");