summaryrefslogtreecommitdiff
path: root/library.cpp
blob: 2386c0b6c7b9b849798b7ced51e3654e405c9c5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#define _GNU_SOURCE //vasprintf
#include <iostream>
#include <cstdarg>
#include <cassert>
#include <SFML/Graphics.hpp>
#include "global.h"
#include "library.h"

using namespace std;


static const Font *currentFont=nullptr;
static sf::Text sharedSfText;

class Init{
public:
	Init(){
		sharedSfText.setFillColor(sf::Color::Black);
		sharedSfText.setCharacterSize(14);
	}
} init_object;


void instance_destroy(Object *obj){
	assert(global.objects.find(obj)!=global.objects.end());
	obj->destroy();
	global.objects_todelete.push_back(obj);
}


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){
	draw_text(x,y,s,strlen(s));
}

__attribute__((format (printf, 3, 4)))
void draw_textf(int x,int y,const char *format,...){
	va_list ap;
	va_start(ap,format);
	char *buf;
	int len=vasprintf(&buf,format,ap);
	va_end(ap);
	assert(len>=0);
	draw_text(x,y,buf,len);
	free(buf);
}


void draw_set_font(const Font *font){
	currentFont=font;
}


static void log(const char *buf,size_t len){
	cerr<<"[LOG] ";
	cerr.write(buf,len);
	cerr<<endl;
}

void log(const char *s){
	log(s,strlen(s));
}

__attribute__((format (printf, 1, 2)))
void logf(const char *format,...){
	va_list ap;
	va_start(ap,format);
	char *buf;
	int len=vasprintf(&buf,format,ap);
	va_end(ap);
	assert(len>=0);
	log(buf,len);
	free(buf);
}