-
Notifications
You must be signed in to change notification settings - Fork 0
/
App_OnInit.cpp
118 lines (94 loc) · 3.26 KB
/
App_OnInit.cpp
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
#include "App.h"
bool App::OnInit(){
prev_frame_time = SDL_GetTicks();
//Initialize SDL
if( SDL_Init( SDL_INIT_VIDEO ) < 0 )
{
SDL_Log( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
return false;
}
//test
SDL_DisplayMode displayMode;
if( SDL_GetCurrentDisplayMode( 0, &displayMode ) == 0 ){
SDL_Log("Screen size: %d, %d", displayMode.w, displayMode.h);
} else {
SDL_Log("Could not get current display mode! SDL_Error: %s\n", SDL_GetError());
}
#ifdef __ANDROID__
SDL_Log("I am an android program\n");
screen_width = displayMode.w;
screen_height = displayMode.h;
#else
SDL_Log("I am a computer program\n");
screen_width = 480;
screen_height = 640;
#endif
//Create window
window = SDL_CreateWindow( "Match 3", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, screen_width, screen_height, SDL_WINDOW_SHOWN | SDL_WINDOW_RESIZABLE );
if( window == NULL )
{
SDL_Log( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
return false;
}
SDL_SetWindowMinimumSize(window, 175, 200);
/* Not using images
int imgFlags = IMG_INIT_PNG;
if( !( IMG_Init( imgFlags ) & imgFlags ) ){
SDL_Log( "SDL_image could not initialize! SDL_image Error: %s\n", IMG_GetError() );
return false;
}*/
renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
if(renderer==NULL){
SDL_Log( "Renderer could not be created! SDL_Error: %s\n", SDL_GetError() );
return false;
}
//SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1");
//SDL_Log("SDL_HINT_RENDER_SCALE_QUALITY: %s\n", SDL_GetHint(SDL_HINT_RENDER_SCALE_QUALITY));
board.OnInit(renderer, 8, 12);
board.setPosSize(screen_margin, screen_margin, screen_width-2*screen_margin, screen_height-2*screen_margin);
return true;
}
// Not using images
#ifdef _SDL_IMAGE_H
SDL_Surface* App::loadSurface( char* path )
{
//The final optimized image
SDL_Surface* optimizedSurface = NULL;
SDL_Surface* loadedSurface = IMG_Load(path);
if( loadedSurface == NULL )
{
SDL_Log( "Unable to load image %s! SDL_Error: %s\n", path, SDL_GetError() );
return NULL;
}
//Convert surface to screen format
optimizedSurface = SDL_ConvertSurface( loadedSurface, screenSurface->format, 0x0 );
if( optimizedSurface == NULL )
{
SDL_Log( "Unable to optimize image %s! SDL Error: %s\n", path, SDL_GetError() );
return NULL;
}
//Get rid of old loaded surface
SDL_FreeSurface( loadedSurface );
return optimizedSurface;
}
SDL_Texture* App::loadTexture( char* path ){
//The final texture
SDL_Texture* newTexture = NULL;
//Load image at specified path
SDL_Surface* loadedSurface = IMG_Load( path );
if( loadedSurface == NULL )
{
SDL_Log( "Unable to load image %s! SDL_image Error: %s\n", path, IMG_GetError() );
return NULL;
}
//Create texture from surface pixels
newTexture = SDL_CreateTextureFromSurface( renderer, loadedSurface );
if( newTexture == NULL )
{
SDL_Log( "Unable to create texture from %s! SDL Error: %s\n", path, SDL_GetError() );
}
//Get rid of old loaded surface
SDL_FreeSurface( loadedSurface );
return newTexture;
}
#endif