Debian Codegraph @

src/engine/core.cpp

raw symbols SCIP
1 /***************************************************************************
2 * fheroes2: https://github.com/ihhub/fheroes2 *
3 * Copyright (C) 2021 - 2024 *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
19 ***************************************************************************/
20
21 #include "core.h"
22
23 #include <cassert>
24 #include <cstdint>
25 #include <stdexcept>
26
27 // Managing compiler warnings for SDL headers
28 #if defined( __GNUC__ )
29 #pragma GCC diagnostic push
30
31 #pragma GCC diagnostic ignored "-Wdouble-promotion"
32 #pragma GCC diagnostic ignored "-Wold-style-cast"
33 #pragma GCC diagnostic ignored "-Wswitch-default"
34 #endif
35
36 #include <SDL.h>
37 #include <SDL_error.h>
38
39 // Managing compiler warnings for SDL headers
40 #if defined( __GNUC__ )
41 #pragma GCC diagnostic pop
42 #endif
43
44 #include "audio.h"
45 #include "localevent.h"
46 #include "logging.h"
47
48 #if defined( TARGET_PS_VITA )
49 #include <psp2/kernel/processmgr.h>
50 #include <psp2/power.h>
51
52 // PlayStation Vita requires definition of such global variable for memory usage.
53 int _newlib_heap_size_user = 192 * 1024 * 1024;
54 #endif
55
56 namespace
57 {
58 #if defined( TARGET_PS_VITA )
59 void initHardwareInternally()
60 {
61 // CPU clock speed, MHz. Possible values: 41, 83, 111, 166, 222, 333, 444, 500
62 scePowerSetArmClockFrequency( 444 );
63 // CPU memory clock speed, MHz. Possible values: 55, 83, 111, 166, 222
64 scePowerSetBusClockFrequency( 222 );
65 // GPU clock speed, MHz. Possible values: 41, 55, 83, 111, 166, 222
66 scePowerSetGpuClockFrequency( 222 );
67 // GPU memory clock speed, MHz. Possible values: 83, 111, 166
68 scePowerSetGpuXbarClockFrequency( 166 );
69 }
70
71 void freeHardwareInternally()
72 {
73 sceKernelExitProcess( 0 );
74 }
75 #else
76 void initHardwareInternally()
77 {
78 // Do nothing.
79 }
80
81 void freeHardwareInternally()
82 {
83 // Do nothing.
84 }
85 #endif
86
87 uint32_t convertToSDLFlag( const fheroes2::SystemInitializationComponent component )
88 {
89 switch ( component ) {
90 case fheroes2::SystemInitializationComponent::Audio:
91 return SDL_INIT_AUDIO;
92 case fheroes2::SystemInitializationComponent::Video:
93 return SDL_INIT_VIDEO;
94 case fheroes2::SystemInitializationComponent::GameController:
95 return SDL_INIT_GAMECONTROLLER;
96 default:
97 // Did you add a new component?
98 assert( 0 );
99 break;
100 }
101
102 return 0;
103 }
104
105 uint32_t getSDLInitFlags( const std::set<fheroes2::SystemInitializationComponent> & components )
106 {
107 uint32_t flags = 0;
108 for ( const fheroes2::SystemInitializationComponent component : components ) {
109 flags |= convertToSDLFlag( component );
110 }
111 return flags;
112 }
113
114 // For now only SDL library is supported.
115 bool initCoreInternally( const std::set<fheroes2::SystemInitializationComponent> & components )
116 {
117 const uint32_t sdlFlags = getSDLInitFlags( components );
118
119 if ( SDL_Init( sdlFlags ) < 0 ) {
120 ERROR_LOG( SDL_GetError() )
121 return false;
122 }
123
124 if ( components.count( fheroes2::SystemInitializationComponent::Audio ) > 0 ) {
125 Audio::Init();
126 }
127
128 if ( components.count( fheroes2::SystemInitializationComponent::GameController ) > 0 ) {
129 LocalEvent::Get().initController();
130 }
131
132 LocalEvent::initEventEngine();
133
134 return true;
135 }
136
137 void freeCoreInternally()
138 {
139 if ( fheroes2::isComponentInitialized( fheroes2::SystemInitializationComponent::GameController ) ) {
140 LocalEvent::Get().CloseController();
141 }
142
143 if ( fheroes2::isComponentInitialized( fheroes2::SystemInitializationComponent::Audio ) ) {
144 Audio::Quit();
145 }
146
147 SDL_Quit();
148 }
149
150 bool isComponentInitializedInternally( const fheroes2::SystemInitializationComponent component )
151 {
152 const uint32_t sdlFlag = convertToSDLFlag( component );
153 assert( sdlFlag != 0 );
154
155 return SDL_WasInit( sdlFlag ) != 0;
156 }
157 }
158
159 namespace fheroes2
160 {
161 HardwareInitializer::HardwareInitializer()
162 {
163 initHardwareInternally();
164 }
165
166 HardwareInitializer::~HardwareInitializer()
167 {
168 freeHardwareInternally();
169 }
170
171 CoreInitializer::CoreInitializer( const std::set<SystemInitializationComponent> & components )
172 {
173 if ( !initCoreInternally( components ) ) {
174 throw std::logic_error( "Core module initialization failed." );
175 }
176 }
177
178 CoreInitializer::~CoreInitializer()
179 {
180 freeCoreInternally();
181 }
182
183 bool isComponentInitialized( const SystemInitializationComponent component )
184 {
185 return isComponentInitializedInternally( component );
186 }
187 }