summaryrefslogtreecommitdiff
path: root/example/3d.cpp
blob: 1b7e9e1768738add33e4cd3cbd9e78b96cada3f7 (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
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
#include <unistd.h>
#include <string>

#include "version.h"

#include "cRender.h"
#include "cObject.h"
#include "cObjectHandler.h"
#include "cInput.h"
#include "cWiremesh.h"

//#include "testobject.h"

class testobject : cObject
{
public:
	testobject()
	{
		setSize(10,5);
		cc = 0;

		drawRectangle('#', NULL, {0,0}, {9,4}, _COL_GREEN, _COL_DEFAULT);
	}

	~testobject() { destruct(); }

	virtual void onClick(sPos _pos, unsigned int _button)
	{
		cc++;
		drawText(std::to_string(cc), {2,2}, _COL_RED);

		drawPoint('Q', _pos, _COL_YELLOW);
	}

	virtual void onChar(unsigned char _c) { drawPoint(_c, {1,1}, _COL_BLUE); }
private:
	int cc;
};

int main(int argc, char* argv[])
{
	cRender render(' ', _COL_DEFAULT);
	cObjectHandler handler(&render);
	cObject ver(45,1);
	cWiremesh obj;
	testobject obj2;

	cInput input;

	unsigned int framecounter = 0;
	bool loop = true;

	if(argc > 1)
	{
			loop = false;
	}

	render.render();

	ver.drawText(DATE, {20,0}, _COL_WHITE);
	ver.drawText(VERSTRING, {0,0}, _COL_WHITE);
	int iver = handler.createObject(&ver);
	handler.moveObject(iver, {0,0}, _MOVE_ABSOLUTE);

	int x = 15;
	int y = 15;
	int z = 30;

	obj.addVector({-x,-y,z}, {2*x,0,0}, '+', _COL_RED);
	obj.addVector({-x,-y,z}, {0,2*y,0}, '+', _COL_RED);
	obj.addVector({-x,y,z}, {2*x,0,0}, '+', _COL_RED);
	obj.addVector({x,-y,z}, {0,2*y,0}, '+', _COL_RED);

	obj.addVector({-x,-y,0}, {0,0,z}, ':', _COL_RED);
	obj.addVector({x,-y,0}, {0,0,z}, ':', _COL_RED);
	obj.addVector({-x,y,0}, {0,0,z}, ':', _COL_RED);
	obj.addVector({x,y,0}, {0,0,z}, ':', _COL_RED);

	obj.addVector({-x,-y,0}, {2*x,0,0}, ',', _COL_RED);
	obj.addVector({-x,-y,0}, {0,2*y,0}, ',', _COL_RED);
	obj.addVector({-x,y,0}, {2*x,0,0}, ',', _COL_RED);
	obj.addVector({x,-x,0}, {0,2*y,0}, ',', _COL_RED);
	int imesh = handler.createWiremesh(&obj);

	int iobj2 = handler.createObject((cObject*)&obj2);
	handler.moveObject(iobj2, {3,3}, _MOVE_ABSOLUTE);

	sPos middle = render.getSize();
	middle.x /= 2;
	middle.y /= 2;

	handler.moveWiremesh(imesh,{middle.x,middle.y,0}, _MOVE_ABSOLUTE);

	while( loop )
	{
		sInputEvent ie = input.poll();

		if(ie.type != _EVENT_NULL)
		{
			if(ie.type == _EVENT_KEY)
			{
				switch (ie.c)
				{
					case 'A'://up
						handler.setCameraPosition({0,-1}, _MOVE_RELATIVE);
						break;
					case 'B'://down
						handler.setCameraPosition({0,1}, _MOVE_RELATIVE);
						break;
					case 'C'://right
						handler.setCameraPosition({1,0}, _MOVE_RELATIVE);
						break;
					case 'D'://left
						handler.setCameraPosition({-1,0}, _MOVE_RELATIVE);
						break;
				};
			}
			else if (ie.type == _EVENT_MOUSE)
			{
				if(ie.b == 0)
					handler.clickEvent({ie.x, ie.y}, 0);
			}
			else if (ie.type == _EVENT_CHAR)
			{
				//handler.charEvent(ie.c);
				switch(ie.c)
				{
					case 'w':
						handler.rotateWiremesh(imesh,{-10,0,0});
						break;
					case 's':
						handler.rotateWiremesh(imesh,{10,0,0});
						break;
					case 'a':
						handler.rotateWiremesh(imesh,{0,-10,0});
						break;
					case 'd':
						handler.rotateWiremesh(imesh,{0,10,0});
						break;
					case 'q':
						handler.rotateWiremesh(imesh,{0,0,-10});
						break;
					case 'e':
						handler.rotateWiremesh(imesh,{0,0,10});
						break;
				};
			}
			else if (ie.type == _EVENT_TERM)
			{
				return 0;
			}
		}

		handler.rotateWiremesh(imesh,{1,1,1});

		handler.write();
		render.render();
		framecounter++;

		if(loop)
			usleep(10*1000);
	}

	return 0;
}