-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
732 lines (595 loc) · 14.1 KB
/
main.cpp
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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
#include <SFML/Graphics.hpp>
#include <iostream>
#include <string>
#include <stdexcept>
#include <limits>
#include "mathvector.h"
#include "collision.h"
#define TEXTURE_ARRAY_SIZE 150
#define OBJECT_ARRAY_SIZE 150
#define MESSAGE_QUEUE_SIZE 150
class GameObject;
//Yeah, yeah, I should probably finda better way of dealing with this than a global variable
bool hasFocus;
// Basic component definitions, should be extended with functionality.
class InputComponent
{
public:
virtual ~InputComponent(){};
InputComponent(){};
virtual void update(GameObject &parent) =0;
};
class LogicComponent
{
public:
virtual ~LogicComponent();
virtual void update(GameObject &parent){}
};
class PhysicsComponent
{
protected:
double position[2];
double velocity[2];
double angvel;
public:
enum componentType {CIRLCE, POINT, AABB, OBB, POLYGON};
componentType type;
GameObject *storedParent;
virtual ~PhysicsComponent(){};
PhysicsComponent()
{
angvel = 0;
for(int i = 0; i < 2; i++)
{
position[i] = 0;
velocity[i] = 0;
}
}
componentType getType()
{
return type;
}
virtual void update(GameObject &parent)
{
storedParent = &parent;
}
GameObject* getParent()
{
return storedParent;
}
double getX()
{
return position[0];
}
double getY()
{
return position[1];
}
void setPosition(int x, int y)
{
position[0] = x;
position[1] = y;
}
void setX(double newPosition)
{
position[0] = newPosition;
}
void setY(double newPosition)
{
position[1] = newPosition;
}
double getAngVel()
{
return angvel;
}
double getXVel()
{
return velocity[0];
}
double getYVel()
{
return velocity[1];
}
void setXVel(double newVel)
{
velocity[0] = newVel;
}
void setYVel(double newVel)
{
velocity[1] = newVel;
}
};
class GraphicsComponent
{
public:
GraphicsComponent(){};
virtual ~GraphicsComponent(){};
virtual void update(GameObject &parent, sf::RenderWindow &window) =0;
};
//Basic game object.
class GameObject
{
private:
std::vector<InputComponent *> inputComponents;
std::vector<LogicComponent *> logicComponents;
std::vector<PhysicsComponent *> physicsComponents;
std::vector<GraphicsComponent *> graphicsComponents;
public:
GameObject()
{
}
~GameObject()
{
for(std::vector<InputComponent *>::iterator it = inputComponents.begin(); it != inputComponents.end(); it++)
{
delete (*it);
}
for(std::vector<LogicComponent *>::iterator it = logicComponents.begin(); it != logicComponents.end(); it++)
{
delete(*it);
}
for(std::vector<PhysicsComponent *>::iterator it = physicsComponents.begin(); it != physicsComponents.end(); it++)
{
delete(*it);
}
for(std::vector<GraphicsComponent *>::iterator it = graphicsComponents.begin(); it != graphicsComponents.end(); it++)
{
delete(*it);
}
}
void update(sf::RenderWindow& window)
{
for(std::vector<InputComponent *>::iterator it = inputComponents.begin(); it != inputComponents.end(); it++)
{
(*it)->update(*this);
}
for(std::vector<LogicComponent *>::iterator it = logicComponents.begin(); it != logicComponents.end(); it++)
{
(*it)->update(*this);
}
for(std::vector<PhysicsComponent *>::iterator it = physicsComponents.begin(); it != physicsComponents.end(); it++)
{
(*it)->update(*this);
}
for(std::vector<GraphicsComponent *>::iterator it = graphicsComponents.begin(); it != graphicsComponents.end(); it++)
{
(*it)->update(*this, window);
}
}
void addInputComponent(InputComponent *newComponent)
{
inputComponents.insert(inputComponents.end(), newComponent);
}
void addLogicComponent(LogicComponent *newComponent)
{
logicComponents.insert(logicComponents.end(), newComponent);
}
void addPhysicsComponent(PhysicsComponent *newComponent)
{
physicsComponents.insert(physicsComponents.end(), newComponent);
}
void addGraphicsComponent(GraphicsComponent *newComponent)
{
graphicsComponents.insert(graphicsComponents.end(), newComponent);
}
InputComponent* getInputComponento(int index)
{
return inputComponents.at(index);
}
LogicComponent* getLogicComponent(int index)
{
return logicComponents.at(index);
}
PhysicsComponent* getPhysicsComponent(int index)
{
return physicsComponents.at(index);
}
GraphicsComponent* getGraphicsComponent(int index)
{
return graphicsComponents.at(index);
}
};
//Declarations of derived component classes
class BasicSpriteComponent: public GraphicsComponent
{
private:
sf::Sprite componentSprite;
public:
~BasicSpriteComponent(){};
BasicSpriteComponent(sf::Texture *texture)
{
componentSprite.setTexture(*texture);
}
void update(GameObject &parent, sf::RenderWindow &window)
{
//std::cout << "Sprite update called!" << std::endl;
componentSprite.setPosition(parent.getPhysicsComponent(0)->getX(),parent.getPhysicsComponent(0)->getY());
window.draw(componentSprite);
}
};
class PlayerInputComponent: public InputComponent
{
public:
~PlayerInputComponent(){};
void update(GameObject &parent)
{
if(hasFocus)
{
//std::cout << "Updating player input!" << std::endl;
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Left))
{
parent.getPhysicsComponent(0)->setXVel(parent.getPhysicsComponent(0)->getXVel() - 2);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Right))
{
parent.getPhysicsComponent(0)->setXVel(parent.getPhysicsComponent(0)->getXVel() + 2);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Up))
{
parent.getPhysicsComponent(0)->setYVel(parent.getPhysicsComponent(0)->getYVel() -2);
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Down))
{
parent.getPhysicsComponent(0)->setYVel(parent.getPhysicsComponent(0)->getYVel() + 2);
}
}
}
};
class PlayerPhysicsComponent: public PhysicsComponent
{
private:
bool hasStoredParent;
int maxVelocity;
int parentOffset;
public:
~PlayerPhysicsComponent(){};
PlayerPhysicsComponent()
{
hasStoredParent = false;
maxVelocity = 50;
parentOffset = 50;
}
void update(GameObject &parent)
{
if(hasStoredParent == false)
{
storedParent = &parent;
}
if(velocity[0] > maxVelocity)
{
velocity[0] = maxVelocity;
}
if(velocity[0] < -maxVelocity)
{
velocity[0] = -maxVelocity;
}
if(velocity[1] > maxVelocity)
{
velocity[1] = maxVelocity;
}
if(velocity[1] < -maxVelocity)
{
velocity[1] = -maxVelocity;
}
if(position[0] <= 0 || position[0] >= 600 - parentOffset)
{
position[0] -= velocity[0];
velocity[0] *= -1;
}
if(position[1] < 0 || position[1] >= 600 - parentOffset)
{
position[1] -= velocity[1];
velocity[1] *= -1;
}
position[0] += velocity[0];
position[1] += velocity[1];
if(velocity[0] > 0)
{
velocity[0] -= 1;
}
if(velocity[1] > 0)
{
velocity[1] -= 1;
}
if(velocity[0] < 0)
{
velocity[0] += 1;
}
if(velocity[1] < 0)
{
velocity[1] += 1;
}
}
};
class ConvexPolygon: public PhysicsComponent
{
private:
sf::ConvexShape polygon;
int maxVelocity;
public:
~ConvexPolygon(){};
ConvexPolygon()
{
maxVelocity = 50;
type = POLYGON;
}
ConvexPolygon(std::vector<MathVector> vertices)
{
type = POLYGON;
maxVelocity = 50;
polygon.setPointCount(vertices.size());
for(int i = 0; i < polygon.getPointCount(); i++)
{
polygon.setPoint(i,sf::Vector2f(vertices[i].getX(), vertices[i].getY()));
}
}
ConvexPolygon(sf::ConvexShape shape)
{
type = POLYGON;
maxVelocity = 50;
polygon = shape;
}
void update(GameObject &parent)
{
if(velocity[0] > maxVelocity)
{
velocity[0] = maxVelocity;
}
if(velocity[0] < -maxVelocity)
{
velocity[0] = -maxVelocity;
}
if(velocity[1] > maxVelocity)
{
velocity[1] = maxVelocity;
}
if(velocity[1] < -maxVelocity)
{
velocity[1] = -maxVelocity;
}
position[0] += velocity[0];
position[1] += velocity[1];
if(velocity[0] > 0)
{
velocity[0] -= 1;
}
if(velocity[1] > 0)
{
velocity[1] -= 1;
}
if(velocity[0] < 0)
{
velocity[0] += 1;
}
if(velocity[1] < 0)
{
velocity[1] += 1;
}
polygon.setPosition(position[0],position[1]);
}
MathVector getWorldSpacePoint(int i)
{
//Only handles translation as of now. Will update when I actually understand transform matrices.
sf::Transform transformMatrix = polygon.getTransform();
MathVector point = MathVector(polygon.getPoint(i));
MathVector newVector = MathVector(transformMatrix.transformPoint(point.toSFMLVector()));
return newVector;
}
std::vector<MathVector> toVector()
{
std::vector<MathVector> returnVector;
for(int i = 0; i < polygon.getPointCount(); i++)
{
returnVector.push_back(getWorldSpacePoint(i));
}
return returnVector;
}
void setColor(sf::Color newColor)
{
polygon.setFillColor(newColor);
}
sf::ConvexShape getConvexShape()
{
return polygon;
}
};
//Generic message class
class Message
{
private:
std::string contents;
public:
Message(){}
Message(std::string message)
{
contents = message;
}
std::string getContents()
{
return contents;
}
void setContents(std::string message)
{
contents = message;
}
};
template <class message_t>
class RingBuffer
{
private:
int head, tail, storedMessages;
message_t buffer[MESSAGE_QUEUE_SIZE];
public:
RingBuffer()
{
head = tail = storedMessages = 0;
}
int addMessage(message_t newMessage)
{
if(storedMessages < MESSAGE_QUEUE_SIZE)
{
buffer[tail] = newMessage;
storedMessages += 1;
if(tail < MESSAGE_QUEUE_SIZE)
{
tail += 1;
} else
{
tail = 0;
}
return 0;
} else
{
std::cout << "Unable to add message to buffer, No remaining space." << std::endl;
return 1;
}
}
message_t getMessage()
{
if(storedMessages != 0)
{
storedMessages -= 1;
if(head < MESSAGE_QUEUE_SIZE)
{
head += 1;
return buffer[head-1];
} else
{
head = 0;
return buffer[head];
}
} else
{
throw std::range_error("No message in buffer to return.");
}
}
int getCurrentQuantity()
{
return storedMessages;
}
};
void loadTexture(sf::Texture textures[], int index, std::string name)
{
sf::Texture tempTexture;
if(!tempTexture.loadFromFile(name))
{
std::cout << "unable to load texture" << name << std::endl;
} else
{
textures[index] = tempTexture;
}
}
MathVector projectPointOntoLine(MathVector point, MathVector vertex1, MathVector vertex2)
{
MathVector e1 = vertex2.subtractVectors(vertex1);
MathVector e2 = point.subtractVectors(vertex1);
double valDP = e1.dotProduct(e2);
double e1Length = e1.magnitude();
double e2Length = e2.magnitude();
double cos = valDP / (e1Length*e2Length);
double projLenOfLine = cos * e2Length;
MathVector p = MathVector(vertex1.getX() + (projLenOfLine * e1.getX()) / e1Length,
vertex1.getY() + (projLenOfLine * e1.getY()) / e1Length);
return p;
}
int main()
{
//Perform initialization
sf::RenderWindow window(sf::VideoMode(600, 600), "WIP SFML Game Engine");
sf::Texture textures[TEXTURE_ARRAY_SIZE];
std::vector<GameObject*> gameObjectsArray;
GameObject *tempObject;
sf::Sprite backgroundSprite;
window.setFramerateLimit(60);
loadTexture(textures, 1, "textures/background.png");
backgroundSprite.setTexture(textures[1]);
std::vector<MathVector> testVector;
testVector.push_back(MathVector(40,90));
testVector.push_back(MathVector(40,50));
testVector.push_back(MathVector(100,110));
ConvexPolygon *testPolygon0 = new ConvexPolygon(testVector);
testPolygon0->setColor(sf::Color(255,0,0));
testPolygon0->setPosition(100,100);
tempObject = new GameObject();
gameObjectsArray.push_back(tempObject);
tempObject->addPhysicsComponent(testPolygon0);
tempObject->addInputComponent(new PlayerInputComponent());
testVector[0] = MathVector(70,30);
testVector[1] = MathVector(50,70);
testVector[2] = MathVector(120,70);
testVector.push_back(MathVector(100,20));
ConvexPolygon testPolygon1 = ConvexPolygon(testVector);
testPolygon1.setPosition(20,20);
testPolygon1.update(*tempObject);
testPolygon1.setColor(sf::Color(0,255,0));
std::cout << "Reached main loop" << std::endl;
while(window.isOpen())
{
hasFocus = window.hasFocus();
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
{
window.close();
}
}
minkowskiDifference_t testCollision = buildMinkowskiDifference(testPolygon0->toVector(), testPolygon1.toVector());
if(testCollision.colliding)
{
std::cout << "Collision detected!" << std::endl;
std::cout << "Collision normal: " << testCollision.collisionNormal.getX() << ", " << testCollision.collisionNormal.getY() << std::endl;
std::cout << "Collision depth: " << testCollision.collisionDepth << std::endl;
}
window.clear();
window.draw(backgroundSprite);
for(std::vector<GameObject*>::iterator it = gameObjectsArray.begin(); it != gameObjectsArray.end(); it++)
{
(*it)->update(window);
}
window.draw(testPolygon0->getConvexShape());
window.draw(testPolygon1.getConvexShape());
window.display();
}
/*
// Dummied out code for testing the ring buffer
RingBuffer<Message> testBuffer;
std::string testString, firstWord, messageSubstring;
std::size_t foundLocation;
Message testMessage;
std::cout << "Type add and then a message to add something to the buffer, type get to get the buffer head." << std::endl;
while(1)
{
std::getline(std::cin, testString);
foundLocation = testString.find(" ");
firstWord = testString.substr(0,foundLocation);
// std::cout << firstWord << std::endl;
if(!firstWord.compare("add"))
{
messageSubstring = testString.substr(foundLocation + 1);
std::cout << "testMessage containing " << messageSubstring << " created, sending to buffer." << std::endl;
testMessage = Message(messageSubstring);
testBuffer.addMessage(testMessage);
std::cout << "new buffer capacity is " << testBuffer.getCurrentQuantity() << std::endl;
}
if(!firstWord.compare("get") && foundLocation == std::string::npos)
{
try
{
testMessage = testBuffer.getMessage();
std::cout << testMessage.getContents() << std::endl;
}
catch (std::exception& e)
{
std::cout << "Exception caught: " << std::endl;
std::cout << e.what() <<std::endl;
}
}
}
*/
return 0;
}