zenilib: zenilib/jni/external/zenilib/zeni_core/Core.cpp Source File

zenilib 0.5.3.0

- Main Page
- Modules
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
Core.cpp
Go to the documentation of this file.
1 /* This file is part of the Zenipex Library (zenilib).
2 * Copyright (C) 2011 Mitchell Keith Bloch (bazald).
3 *
4 * zenilib is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
8 *
9 * zenilib is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with zenilib. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #include <zeni_core.h>
19
20 #ifdef ANDROID
21 #include <android/window.h>
22 #endif
23
24 #include <SDL/SDL.h>
25
26 #include <iostream>
27 #include <fstream>
28 #include <cassert>
29
30 #if defined(_DEBUG) && defined(_WINDOWS)
31 #define DEBUG_NEW new(_NORMAL_BLOCK, __FILE__, __LINE__)
32 #define new DEBUG_NEW
33 #endif
34
35 #include <Zeni/Singleton.hxx>
36
37 namespace Zeni {
38
39 template class Singleton<Core>;
40
41 Core * Core::create() {
42 return new Core;
43 }
44
45 Core::Core()
46 #ifdef ANDROID
47 : m_screen_saver_enabled(true)
48 #endif
49 {
50 #ifdef ANDROID
51 if(!g_state)
52 throw Core_Init_Failure();
53 #endif
54
55 get_File_Ops(); // Redirect File I/O
56
59 #ifndef ANDROID
60 if(SDL_Init(SDL_INIT_TIMER) == -1)
61 throw Core_Init_Failure();
62 #endif
63 }
64
65 Core::~Core() {
66 #ifndef ANDROID
67 SDL_Quit();
68 #endif
69 }
70
71 Core & get_Core() {
72 return Core::get();
73 }
74
75 #ifdef ANDROID
76 void Core::preinit(android_app * const &state_) {
77 g_state = state_;
78 }
79
80 android_app & Core::get_state() {
81 return *g_state;
82 }
83 #endif
84
85 bool Core::is_screen_saver_enabled() {
86 #ifdef ANDROID
87 return m_screen_saver_enabled;
88 #elif SDL_VERSION_ATLEAST(1,3,0)
89 return SDL_IsScreenSaverEnabled() != 0;
90 #elif defined(_WINDOWS)
91 BOOL is_active;
92 SystemParametersInfo(SPI_GETSCREENSAVEACTIVE, 0, &is_active, 0);
93 return is_active != 0;
94 #else
95 return true;
96 #endif
97 }
98
99 void Core::set_screen_saver(const bool &
100 #if defined(ANDROID) || SDL_VERSION_ATLEAST(1,3,0) || defined(_WINDOWS)
101 enabled
102 #endif
103 )
104 {
105 #if defined(ANDROID)
106 m_screen_saver_enabled = enabled;
107
108 if(enabled)
109 ANativeActivity_setWindowFlags(g_state->activity, 0, AWINDOW_FLAG_KEEP_SCREEN_ON);
110 else
111 ANativeActivity_setWindowFlags(g_state->activity, AWINDOW_FLAG_KEEP_SCREEN_ON, 0);
112 #elif SDL_VERSION_ATLEAST(1,3,0)
113 if(enabled)
114 SDL_EnableScreenSaver();
115 else
116 SDL_DisableScreenSaver();
117 #elif defined(_WINDOWS)
118 SystemParametersInfo(SPI_SETSCREENSAVEACTIVE, UINT(enabled), 0, SPIF_SENDCHANGE);
119 #endif
120 }
121
122 void Core::assert_no_error() {
123 #ifndef ANDROID
124 if(strlen(SDL_GetError())) {
125 std::cerr << "SDL : " << SDL_GetError() << std::endl;
126 SDL_ClearError();
127 assert(false);
128 }
129 #endif
130 }
131
132 bool Core::print_error() {
133 #ifndef ANDROID
134 if(strlen(SDL_GetError())) {
135 std::cerr << "SDL : " << SDL_GetError() << std::endl;
136 SDL_ClearError();
137 return true;
138 }
139 #endif
140
141 return false;
142 }
143
144 #ifdef ANDROID
145 android_app *Core::g_state = 0;
146 #endif
147
148 }
Zeni::Core::assert_no_error
static void assert_no_error()
If there is an SDL error, print it and assert(false)
Definition: Core.cpp:122
enabled
GLenum GLsizei const GLuint GLboolean enabled
Definition: glew.h:2538
assert
#define assert(x)
Definition: SDL_malloc.c:1234
SDL_Quit
DECLSPEC void SDLCALL SDL_Quit(void)
Definition: SDL.c:342
UINT
typedef UINT(WINAPI *PFNWGLGETCONTEXTGPUIDAMDPROC)(HGLRC hglrc)
SDL_GetError
DECLSPEC const char *SDLCALL SDL_GetError(void)
Definition: SDL_error.c:204
SDL_VERSION_ATLEAST
#define SDL_VERSION_ATLEAST(X, Y, Z)
Definition: SDL_version.h:106
android_app
Definition: android_native_app_glue.h:111
Zeni::Core
The Core Singleton.
Definition: Core.h:53
Zeni::Core::set_screen_saver
void set_screen_saver(const bool &enabled)
Enable/Disable the screen saver.
Definition: Core.cpp:99
Singleton.hxx
Zeni::Singleton< Core >::get
static Core & get()
Zeni::Core::is_screen_saver_enabled
bool is_screen_saver_enabled()
Check to see if the screen saver is enabled.
Definition: Core.cpp:85
SDL_IsScreenSaverEnabled
DECLSPEC SDL_bool SDLCALL SDL_IsScreenSaverEnabled(void)
Returns whether the screensaver is currently enabled (default on).
Definition: SDL_video.c:2226
SDL_EnableScreenSaver
DECLSPEC void SDLCALL SDL_EnableScreenSaver(void)
Allow the screen to be blanked by a screensaver.
Definition: SDL_video.c:2235
zeni_core.h
SDL_ClearError
DECLSPEC void SDLCALL SDL_ClearError(void)
Definition: SDL_error.c:212
Zeni::Core::print_error
static bool print_error()
If there is an SDL error, print it and then clear it; returns true iff there was an error...
Definition: Core.cpp:132
Zeni::get_Core
Core & get_Core()
Get access to the singleton.
Definition: Core.cpp:71
SDL_DisableScreenSaver
DECLSPEC void SDLCALL SDL_DisableScreenSaver(void)
Prevent the screen from being blanked by a screensaver.
Definition: SDL_video.c:2250
SDL_INIT_TIMER
#define SDL_INIT_TIMER
Definition: SDL.h:108
SDL_Init
DECLSPEC int SDLCALL SDL_Init(Uint32 flags)
Definition: SDL.c:235
Zeni::get_File_Ops
File_Ops & get_File_Ops()
Get access to the singleton.
Definition: File_Ops.cpp:118
BOOL
typedef BOOL(WINAPI *PFNWGLSETSTEREOEMITTERSTATE3DLPROC)(HDC hDC