-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_1.c
133 lines (107 loc) · 3.77 KB
/
example_1.c
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
/*
example_1.c
- demonstrates loading APIs
- demonstrates hot reloading
*/
/*
Index of this file:
// [SECTION] includes
// [SECTION] structs
// [SECTION] apis
// [SECTION] pl_app_load
// [SECTION] pl_app_shutdown
// [SECTION] pl_app_resize
// [SECTION] pl_app_update
*/
//-----------------------------------------------------------------------------
// [SECTION] includes
//-----------------------------------------------------------------------------
#include <stdlib.h> // malloc, free
#include <stdio.h>
#include <string.h> // memset
#include "pl.h"
#include "pl_window_ext.h"
//-----------------------------------------------------------------------------
// [SECTION] structs
//-----------------------------------------------------------------------------
typedef struct _plAppData
{
plWindow* ptWindow;
} plAppData;
//-----------------------------------------------------------------------------
// [SECTION] apis
//-----------------------------------------------------------------------------
const plIOI* gptIO = NULL;
const plWindowI* gptWindows = NULL;
//-----------------------------------------------------------------------------
// [SECTION] pl_app_load
//-----------------------------------------------------------------------------
PL_EXPORT void*
pl_app_load(plApiRegistryI* ptApiRegistry, plAppData* ptAppData)
{
// NOTE: on first load, "pAppData" will be NULL but on reloads
// it will be the value returned from this function
// retrieve the data registry API, this is the API used for sharing data
// between extensions & the runtime
const plDataRegistryI* ptDataRegistry = pl_get_api_latest(ptApiRegistry, plDataRegistryI);
// load required apis
gptIO = pl_get_api_latest(ptApiRegistry, plIOI);
gptWindows = pl_get_api_latest(ptApiRegistry, plWindowI);
// if "ptAppData" is a valid pointer, then this function is being called
// during a hot reload.
if(ptAppData)
{
printf("Hot reload!\n");
// return the same memory again
return ptAppData;
}
// this path is taken only during first load, so we
// allocate app memory here
ptAppData = malloc(sizeof(plAppData));
memset(ptAppData, 0, sizeof(plAppData));
// use window API to create a window
plWindowDesc tWindowDesc = {
.pcTitle = "Example 1",
.iXPos = 200,
.iYPos = 200,
.uWidth = 600,
.uHeight = 600,
};
gptWindows->create_window(tWindowDesc, &ptAppData->ptWindow);
// return app memory
return ptAppData;
}
//-----------------------------------------------------------------------------
// [SECTION] pl_app_shutdown
//-----------------------------------------------------------------------------
PL_EXPORT void
pl_app_shutdown(plAppData* ptAppData)
{
// perform any cleanup here
gptWindows->destroy_window(ptAppData->ptWindow);
// free app memory
free(ptAppData);
}
//-----------------------------------------------------------------------------
// [SECTION] pl_app_resize
//-----------------------------------------------------------------------------
PL_EXPORT void
pl_app_resize(plAppData* ptAppData)
{
// perform any operations required during a window resize
plIO* ptIO = gptIO->get_io();
printf("resize to %d, %d\n", (int)ptIO->tMainViewportSize.x, (int)ptIO->tMainViewportSize.y);
}
//-----------------------------------------------------------------------------
// [SECTION] pl_app_update
//-----------------------------------------------------------------------------
PL_EXPORT void
pl_app_update(plAppData* ptAppData)
{
gptIO->new_frame(); // must be called once at the beginning of a frame
// check for key press
if(gptIO->is_key_pressed(PL_KEY_P, true))
{
printf("P key pressed!\n");
}
}