FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
orx/tutorial/src/10_Locale.cpp at master · orx/orx · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
orx
/
orx
Public
Uh oh!
There was an error while loading.
Please reload this page
.
Notifications
You must be signed in to change notification settings
Fork
123
Star
1.8k
Code
Issues
2
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
orx
/
tutorial
/
src
/
10_Locale.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
306 lines (248 loc) · 8.56 KB
Breadcrumbs
orx
/
tutorial
/
src
/
10_Locale.cpp
Copy path
File metadata and controls
306 lines (248 loc) · 8.56 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
/*
Orx - Portable Game Engine
*
* Copyright (c) 2008- Orx-Project
*
* This software is provided 'as-is', without any express or implied
* warranty. In no event will the authors be held liable for any damages
* arising from the use of this software.
*
* Permission is granted to anyone to use this software for any purpose,
* including commercial applications, and to alter it and redistribute it
* freely, subject to the following restrictions:
*
* 1. The origin of this software must not be misrepresented; you must not
* claim that you wrote the original software. If you use this software
* in a product, an acknowledgment in the product documentation would be
* appreciated but is not required.
*
* 2. Altered source versions must be plainly marked as such, and must not be
* misrepresented as being the original software.
*
* 3. This notice may not be removed or altered from any source
* distribution.
*/
/*
*
* @file 10_Locale.cpp
* @date 30/08/2008
* @author iarwain@orx-project.org
*
* Locale / C++ tutorial
*/
/*
This is a basic C++ tutorial showing how to write a program in C++ using orx.
*
* See previous tutorials for more info about the basic object creation, clock, animation,
* viewport, sound, FX, physics/collision and parallax scrolling handling.
*
* This tutorial shows how to create your game using C++ and orx as a library.
* This code is a basic C++ example to show how to use orx without having to write C code.
*
* This tutorial could have been architectured in a better way (cutting it into pieces with headers files, for example)
* but we wanted to keep a single file per *basic* tutorial.
*
* This program also creates a console (as does the default orx executable),
* but you can have you own console-less program if you wish. In order to achieve that, you only need to provide
* an argc/argv style parameter list that contains the executable name, otherwise the default loaded config file will be
* orx.ini instead of being based on our executable name (ie. 10_Locale.ini).
*
* This tutorial simply display orx's logo and a localized legend. Press space to cycle through
* all the availables languages for the legend's text.
*
* Some explanations about core elements that you can find in this tutorial:
*
* - Run function: Don't put *ANY* logic code here, it's only a backbone where you can handle default
* core behaviors (tracking exit or changing locale, for example) or profile some stuff.
* As it's directly called from the main loop and not part of the clock system, time consistency can't be enforced.
* For all your main game execution, please create (or use an existing) clock and register your callback to it.
*
* - Event handlers:
* When an event handler returns orxSTATUS_SUCCESS, no other handler will be called after it
* for the same event.
* On the other hand, if orxSTATUS_FAILURE is returned, event processing will continue for this event
* if other handlers are listening this event type.
* We'll monitor locale events to update our legend's text when the selected language is changed.
*
* - orx_Execute():
* Inits and executes orx using our self-defined functions (Init, Run and Exit).
* We can of course not use this helper and handles everything manually if its
* behavior doesn't suit our needs.
* You can have a look at the content of orx_Execute() (which is implemented in orx.h) to have
* a better idea on how to do this.
*/
//
Include orx.h
#
include
"
orx.h
"
#
include
<
iostream
>
//
!!! First we define our logo object !!!
//
Our logo class
class
Logo
{
private:
//
Our orxOBJECT self reference
orxOBJECT *mpstObject;
//
Our orxOBJECT reference to the legend
orxOBJECT *mpstLegend;
public:
Logo
();
~Logo
();
};
//
C-tor
Logo::Logo
()
{
//
Creates and stores our orxOBJECT
mpstObject =
orxObject_CreateFromConfig
(
"
Logo
"
);
//
Adds ourselves to the orxOBJECT
orxObject_SetUserData
(mpstObject,
this
);
//
Stores our legend object (we don't use it in this tutorial, this is done for demonstration purposes)
mpstLegend =
orxObject_GetChild
(mpstObject);
}
//
D-tor
Logo::~Logo
()
{
//
Deletes our orxOBJECT
orxObject_Delete
(mpstObject);
}
//
!!! Game class that contains our core callbacks !!!
//
Game class
//
NB: The callbacks could have been defined out of any class
//
This is just to show how to do it if you need it
class
Game
{
public:
static
orxSTATUS orxFASTCALL
EventHandler
(
const
orxEVENT *_pstEvent);
static
orxSTATUS orxFASTCALL
Init
();
static
void
orxFASTCALL
Exit
();
static
orxSTATUS orxFASTCALL
Run
();
static
void
orxFASTCALL
Update
(
const
orxCLOCK_INFO* _pstClockInfo,
void
* _pstContext);
void
SelectNextLanguage
();
Game
() : m_poLogo(
NULL
), u32LanguageIndex(
0
) {};
~Game
() {};
private:
orxSTATUS
InitGame
();
Logo *m_poLogo;
orxU32 u32LanguageIndex;
};
//
Local instance
Game soMyGame;
//
Select next language
void
Game::SelectNextLanguage
()
{
//
Updates language index
u32LanguageIndex = (u32LanguageIndex ==
orxLocale_GetLanguageCount
() -
1
) ?
0
: u32LanguageIndex +
1
;
//
Selects it
orxLocale_SelectLanguage
(
orxLocale_GetLanguage
(u32LanguageIndex), orxTEXT_KZ_LOCALE_GROUP);
}
//
Init game function
orxSTATUS
Game::InitGame
()
{
orxSTATUS eResult;
//
We register our event handler
eResult =
orxEvent_AddHandler
(orxEVENT_TYPE_LOCALE, EventHandler);
//
Successful?
if
(eResult != orxSTATUS_FAILURE)
{
//
Creates logo
m_poLogo =
new
Logo
();
//
Displays all the available languages, C++ style
std::cout <<
"
The available languages are:
"
<< std::endl;
for
(orxU32 i =
0
; i <
orxLocale_GetLanguageCount
(); i++)
{
std::cout <<
"
-
"
<<
orxLocale_GetLanguage
(i) << std::endl;
}
//
Creates viewport
orxViewport_CreateFromConfig
(
"
Viewport
"
);
}
//
Done!
return
eResult;
}
//
Event handler
orxSTATUS orxFASTCALL
Game::EventHandler
(
const
orxEVENT *_pstEvent)
{
//
Depending on event ID
switch
(_pstEvent->
eID
)
{
case
orxLOCALE_EVENT_SELECT_LANGUAGE:
{
orxLOCALE_EVENT_PAYLOAD *pstPayload;
//
Gets locale payload
pstPayload = (orxLOCALE_EVENT_PAYLOAD *)_pstEvent->
pstPayload
;
//
Logs info
orxLOG
(
"
Switching to '%s'.
"
, pstPayload->
zLanguage
);
break
;
}
default
:
{
break
;
}
}
//
Done!
return
orxSTATUS_SUCCESS;
}
//
Init function
orxSTATUS
Game::Init
()
{
orxSTATUS eResult;
orxINPUT_TYPE eType;
orxENUM eID;
orxINPUT_MODE eMode;
const
orxSTRING zInputQuit;
const
orxSTRING zInputCycle;
/*
Gets input binding names
*/
orxInput_GetBinding
(
"
Quit
"
,
0
, &eType, &eID, &eMode);
zInputQuit =
orxInput_GetBindingName
(eType, eID, eMode);
orxInput_GetBinding
(
"
CycleLanguage
"
,
0
, &eType, &eID, &eMode);
zInputCycle =
orxInput_GetBindingName
(eType, eID, eMode);
//
Logs
orxLOG
(
"
\n
- '%s' will exit from this tutorial
"
"
\n
- '%s' will cycle through all the available languages
"
"
\n
* The legend under the logo is always displayed in the current language
"
, zInputQuit, zInputCycle);
orxLOG
(
"
10_Locale Init() called!
"
);
//
Registers our update function
orxClock_Register
(
orxClock_Get
(orxCLOCK_KZ_CORE), Update, orxNULL, orxMODULE_ID_MAIN, orxCLOCK_PRIORITY_NORMAL);
//
Inits our game
eResult = soMyGame.
InitGame
();
//
Done!
return
eResult;
}
//
Exit function
void
Game::Exit
()
{
//
Deletes our logo
delete
soMyGame.
m_poLogo
;
soMyGame.
m_poLogo
=
NULL
;
//
Logs
orxLOG
(
"
10_Locale Exit() called!
"
);
}
//
Run function
orxSTATUS
Game::Run
()
{
orxSTATUS eResult = orxSTATUS_SUCCESS;
//
Is quit action active?
if
(
orxInput_IsActive
(
"
Quit
"
))
{
//
Logs
orxLOG
(
"
Quit action triggered, exiting!
"
);
//
Sets return value to orxSTATUS_FAILURE, meaning we want to exit
eResult = orxSTATUS_FAILURE;
}
//
Done!
return
eResult;
}
/*
* Update callback
*/
void
orxFASTCALL
Game::Update
(
const
orxCLOCK_INFO* _pstClockInfo,
void
* _pstContext)
{
//
Cycle action is active?
if
(
orxInput_HasBeenActivated
(
"
CycleLanguage
"
))
{
//
Selects next language
soMyGame.
SelectNextLanguage
();
}
}
//
!!! Main program code !!!
//
Main program function
int
main
(
int
argc,
char
**argv)
{
//
Inits and runs orx using our self-defined functions
orx_Execute
(argc, argv, Game::Init, Game::Run, Game::Exit);
//
Done!
return
EXIT_SUCCESS
;
}
Back
|
FazBrowse Home
|
New Git URL