-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
122 lines (98 loc) · 3.14 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
#include "Supernova.h"
using namespace Supernova;
Scene scene;
Image image(&scene);
Container textcontainer(&scene);
Text text1(&scene);
Text text2(&scene);
Scene carscene;
Camera* camera;
Shape terrain(&carscene);
Model car(&carscene);
SkyBox sky(&carscene);
float curve = 0;
float speed = 0;
float rotation = 0;
void onUpdate();
void onKeyDown(int key, bool repeat, int mods);
void init(){
Engine::setCanvasSize(1000, 480);
Engine::setCallTouchInMouseEvent(true);
camera = new Camera(&carscene);
camera->setFramebufferFilter(TextureFilter::NEAREST);
camera->setFramebufferSize(512, 512);
camera->setRenderToTexture(true);
camera->setPosition(0, 5, 20);
carscene.setCamera(camera->getEntity());
terrain.createPlane(200, 200);
terrain.setTexture("road.png");
car.loadModel("jeep/Jeep.obj");
car.setScale(0.5);
sky.setTextureFront("ely_hills/hills_lf.tga");
sky.setTextureBack("ely_hills/hills_rt.tga");
sky.setTextureLeft("ely_hills/hills_bk.tga");
sky.setTextureRight("ely_hills/hills_ft.tga");
sky.setTextureUp("ely_hills/hills_up.tga");
sky.setTextureDown("ely_hills/hills_dn.tga");
image.setTexture(camera->getFramebuffer());
image.setPosition(550,100,0);
image.setSize(400, 300);
image.setAnchorPreset(AnchorPreset::CENTER);
textcontainer.setAnchorPreset(AnchorPreset::CENTER_TOP);
textcontainer.addChild(&text1);
textcontainer.addChild(&text2);
text1.setText("Press F to fullscreen draw rect");
text1.setFontSize(20);
text1.setAnchorPreset(AnchorPreset::CENTER);
text2.setText("Press R to change resolution");
text2.setFontSize(20);
text2.setAnchorPreset(AnchorPreset::CENTER);
Engine::setScene(&scene);
Engine::addSceneLayer(&carscene);
Engine::setScalingMode(Scaling::NATIVE);
Engine::onKeyDown = onKeyDown;
Engine::onUpdate = onUpdate;
}
void onKeyDown(int key, bool repeat, int mods){
if (key == S_KEY_F){
if (image.getAnchorPreset() == AnchorPreset::CENTER){
image.setAnchorPreset(AnchorPreset::FULL_LAYOUT);
}else{
image.setSize(400, 300);
image.setAnchorPreset(AnchorPreset::CENTER);
}
}
if (key == S_KEY_R){
if (camera->getFramebuffer()->getWidth() == 512){
camera->setFramebufferSize(256, 256);
}else{
camera->setFramebufferSize(512, 512);
}
}
}
void onUpdate(){
if (Input::isKeyPressed(S_KEY_UP)){
speed += 0.1;
}else if (Input::isKeyPressed(S_KEY_DOWN)){
speed -= 0.1;
}
speed *= 0.95;
if (Input::isKeyPressed(S_KEY_LEFT)){
if (curve < 3)
curve += 0.4;
}else if (Input::isKeyPressed(S_KEY_RIGHT)){
if (curve > -3)
curve -= 0.4;
}else{
curve = 0;
}
rotation += curve;
if (rotation > 360)
rotation = rotation - 360;
if (rotation < 0)
rotation = 360 + rotation;
Vector3 vDir(cos(Angle::degToRad(rotation-90)), 0, -sin(Angle::degToRad(rotation-90)));
car.setRotation(0,rotation,0);
car.setPosition(car.getPosition() + (vDir*speed));
camera->setView(car.getPosition());
}