-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.cpp
More file actions
129 lines (108 loc) · 4.48 KB
/
Main.cpp
File metadata and controls
129 lines (108 loc) · 4.48 KB
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
#include <unordered_map>
#include <cfloat>
#include "ConnectionToServer.h"
#include "stdafx.h"
#include "@@headers.h"
#include "MyClass.h"
/******************************************************************************/
Vec2 dot_pos(0, 0);
/******************************************************************************/
/******************************************************************************/
// ─────────────── Images ───────────────
static ImagePtr gLogo; // smart-pointer managed by Esenthel
/******************************************************************************/
void InitPre() // initialize before engine inits
{
//App.flag=APP_ALLOW_NO_XDISPLAY|APP_ALLOW_NO_GPU;
#ifdef DEBUG
App.flag|=APP_MEM_LEAKS|APP_BREAKPOINT_ON_ERROR;
#endif
App.flag|=APP_WORK_IN_BACKGROUND; // keep running when unfocused
App.background_wait=0; // no delay when in background
App.name("Client");
LogName("log_client.txt");
INIT(); // call auto-generated function that will set up application name, load engine and project data
LogConsole(true);
LogN(S+"InitPre()");
}
/******************************************************************************/
bool Init() // initialize after engine is ready
{
LogN(S+"Init()");
SetupEnet(); // <<< NEW
// ── grab the texture from the asset cache ──
gLogo = ASSET_LOGO;
if(!gLogo) LogN("Logo image failed to load!");
return true;
}
/******************************************************************************/
void Shut() // shut down at exit
{
LogN(S+"Shut()1111");
if(gClientPeer && gClientPeer->state == ENET_PEER_STATE_CONNECTED)
{
enet_peer_disconnect(gClientPeer, 0);
ENetEvent e;
// Pump until disconnect confirmation or timeout
for(int i=0; i<10 && enet_host_service(gClient, &e, 100) > 0; )
{
if(e.type == ENET_EVENT_TYPE_DISCONNECT)
break;
if(e.type == ENET_EVENT_TYPE_RECEIVE)
enet_packet_destroy(e.packet);
// continue servicing until timeout occurs or disconnect event handled
}
enet_host_flush(gClient);
}
if(gClient) enet_host_destroy(gClient);
enet_deinitialize();
}
/******************************************************************************/
bool Update() // main updating
{
// here you have to process each frame update
if(Kb.bp(KB_ESC))return false; // exit if escape on the keyboard pressed
Flt speed = 0.5f; // movement speed
if(Kb.b(KB_LEFT )) dot_pos.x -= speed * Time.d();
if(Kb.b(KB_RIGHT)) dot_pos.x += speed * Time.d();
if(Kb.b(KB_UP )) dot_pos.y += speed * Time.d();
if(Kb.b(KB_DOWN )) dot_pos.y -= speed * Time.d();
/* ── ENet pump ───────────────────────── */
ServiceHost(gClient);
if(gMyId>=0)
{
DotPacket pkt{gMyId, dot_pos.x, dot_pos.y};
ENetPacket *p = enet_packet_create(&pkt, sizeof(pkt), ENET_PACKET_FLAG_UNSEQUENCED);
enet_peer_send(gClientPeer, 0, p);
enet_host_flush(gClient);
}
// send one test packet right after connection succeeds
if(gEnetReady)
{
gEnetReady = false; // only once
static const char8 msg[] = "Hello ENet!"; // was: const char
ENetPacket *p = enet_packet_create(msg, sizeof(msg),
ENET_PACKET_FLAG_RELIABLE);
enet_peer_send(gClientPeer, 0, p);
enet_host_flush(gClient); // push immediately
}
return true; // continue
}
/******************************************************************************/
MyClass myObject("ExampleObject", 42); // Create an instance of MyClass
void Draw() // main drawing
{
D.clear(AZURE); // clear screen to azure color
// ── draw the logo ───────────────────────────────
if(gLogo) {
gLogo->draw(Rect_C(0.0f, 0.5f, 0.3f, 0.3f));
}
D.text (0, 0, "Hello to " ENGINE_NAME " Engine !");
D.text (0, -0.1, S+ "FPS: " + Time.fps());
D.text (0, -0.2, S+ " Arrow keys to move dot, Esc to Exit");
myObject.print(); // Display MyClass details
D.dot(BLUE, dot_pos, 0.02f); // draw moving dot
for(const auto &p : gOtherDots)
if(p.first!=gMyId) D.dot(RED, p.second, 0.02f);
}
/******************************************************************************/