blob: d6a86606f62c83969cdb23cd6a05ad0f7dd9a3cd (
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
|
#include <iostream>
#include <memory>
#include <cstring>
#include <cstdlib>
#include <sys/time.h>
#include <SFML/Graphics.hpp>
#include "object_base.h"
#include "global.h"
#include "library.h"
#include "build/objects.h"
#include "build/sprites.h"
using namespace std;
__attribute__((unused))
static Object* makeobjectbyname(int x,int y,const char *name){
#define X(objname) if(strcmp(name,#objname)==0)return new objname(x,y);
OBJECT_CLASSES_XLIST
#undef X
return nullptr; //no object with that name
}
Global global;
sf::RenderWindow window;
void applyObjectChanges(){
set<Object*> seen;
for(Object *obj : global.objects_todelete){
if(seen.insert(obj).second){
delete obj;
global.objects.erase(obj);
}
}
global.objects_todelete.clear();
}
void applyObjectChanges(set<Object*>::iterator &it){
bool incremented=false;
set<Object*> seen;
for(Object *obj : global.objects_todelete){
if(seen.insert(obj).second){
delete obj;
if(it!=global.objects.end()&&*it==obj){
++it;
incremented=true;
}
global.objects.erase(obj);
}
}
global.objects_todelete.clear();
if(!incremented){
++it;
}
}
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(set<Object*>::iterator it=global.objects.begin();
it!=global.objects.end();
applyObjectChanges(it)){
(*it)->step();
}
window.clear(sf::Color::White);
for(set<Object*>::iterator it=global.objects.begin();
it!=global.objects.end();
applyObjectChanges(it)){
(*it)->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);
instance_create<obj_control>(0,0);
applyObjectChanges();
while(window.isOpen()){
eventCycle();
}
for(set<Object*>::iterator it=global.objects.begin();
it!=global.objects.end();
applyObjectChanges(it)){
instance_destroy(*it);
}
}
|