forked from Nebuleon/hocoslamfy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
init.c
155 lines (141 loc) · 3.93 KB
/
init.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
* Hocoslamfy, initialisation code file
* Copyright (C) 2014 Nebuleon Fumika <nebuleon@gcw-zero.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <stdbool.h>
#include <stddef.h>
#include "SDL.h"
#include "SDL_image.h"
#include "main.h"
#include "init.h"
#include "platform.h"
#include "title.h"
static const char* BackgroundImageNames[BG_LAYER_COUNT] = {
"Sky.png",
"Mountains.png",
"Clouds3.png",
"Clouds2.png",
"Clouds1.png",
"Grass3.png",
"Grass2.png",
"Grass1.png"
};
static bool CheckImage(bool* Continue, bool* Error, const SDL_Surface* Image, const char* Name)
{
if (Image == NULL)
{
*Continue = false; *Error = true;
printf("%s: IMG_Load failed: %s\n", Name, IMG_GetError());
return false;
}
else
{
printf("Successfully loaded %s\n", Name);
return true;
}
}
static SDL_Surface* ConvertSurface(bool* Continue, bool* Error, SDL_Surface* Source, const char* Name)
{
SDL_Surface* Dest;
if (Source->format->Amask != 0)
Dest = SDL_DisplayFormatAlpha(Source);
else
Dest = SDL_DisplayFormat(Source);
if (Dest == NULL)
{
*Continue = false; *Error = true;
printf("%s: SDL_ConvertSurface failed: %s\n", Name, SDL_GetError());
SDL_ClearError();
return NULL;
}
else
{
printf("Successfully converted %s to the screen's pixel format\n", Name);
SDL_FreeSurface(Source);
return Dest;
}
}
void Initialize(bool* Continue, bool* Error)
{
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_JOYSTICK) < 0)
{
*Continue = false; *Error = true;
printf("SDL initialisation failed: %s\n", SDL_GetError());
SDL_ClearError();
return;
}
else
printf("SDL initialisation succeeded\n");
Screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_HWSURFACE |
#ifdef SDL_TRIPLEBUF
SDL_TRIPLEBUF
#else
SDL_DOUBLEBUF
#endif
);
if (Screen == NULL)
{
*Continue = false; *Error = true;
printf("SDL_SetVideoMode failed: %s\n", SDL_GetError());
SDL_ClearError();
return;
}
else
printf("SDL_SetVideoMode succeeded\n");
SDL_ShowCursor(0);
uint32_t i;
for (i = 0; i < BG_LAYER_COUNT; i++)
{
BackgroundImages[i] = IMG_Load(BackgroundImageNames[i]);
if (!CheckImage(Continue, Error, BackgroundImages[i], BackgroundImageNames[i]))
return;
if ((BackgroundImages[i] = ConvertSurface(Continue, Error, BackgroundImages[i], BackgroundImageNames[i])) == NULL)
return;
}
CharacterFrames = IMG_Load("Bee.png");
if (!CheckImage(Continue, Error, CharacterFrames, "Bee.png"))
return;
if ((CharacterFrames = ConvertSurface(Continue, Error, CharacterFrames, "Bee.png")) == NULL)
return;
CollisionImage = IMG_Load("Crash.png");
if (!CheckImage(Continue, Error, CollisionImage, "Crash.png"))
return;
if ((CollisionImage = ConvertSurface(Continue, Error, CollisionImage, "Crash.png")) == NULL)
return;
ColumnImage = IMG_Load("Bamboo.png");
if (!CheckImage(Continue, Error, ColumnImage, "Bamboo.png"))
return;
if ((ColumnImage = ConvertSurface(Continue, Error, ColumnImage, "Bamboo.png")) == NULL)
return;
InitializePlatform();
// Title screen. (-> title.c)
ToTitleScreen();
}
void Finalize()
{
uint32_t i;
for (i = 0; i < BG_LAYER_COUNT; i++)
{
SDL_FreeSurface(BackgroundImages[i]);
BackgroundImages[i] = NULL;
}
SDL_FreeSurface(CharacterFrames);
CharacterFrames = NULL;
SDL_FreeSurface(ColumnImage);
ColumnImage = NULL;
SDL_Quit();
}