Здравствуйте! В рамках знакомства с SDL2 я создаю пробный проект. Одним из ключевых моментов данной работы является использование собственных пространств имен в зависимости от "уровня" приложения. Например, уровень 'Основное меню' должен использовать пространство имен 'menu', уровень 'Настройки' использует пространство имен 'menu::settings' и так далее. Пространства имен описаны в области глобальных переменных и и имеют типовое описание. Файл глобальных переменных 'global_variables.h':
#include "stdafx.h"
//Screen dimension constants
const int SCREEN_WIDTH[] = {800, 1024, 1280};
const int SCREEN_HEIGHT[] = {600, 768, 1024};
short num_of_level = 1; // Counter of levels
// SCR number
short nom_scr = 0; // Screen mode variable;
// 0 - 800x600
// 1 - 1024x768
// 2 - 1280x1024
// Check of resolution changing
bool res_change = false;
// Sound volume
short sound_vol = 0;
// Menu variables
namespace menu
{
const short N = 4;
const short M = 1;
// Positions of button: the First dimension - number of button, the Second dimension is number of times screen resolution
// "START" - button, "LOAD" - button, "SETTINGS" - button, "EXIT" - button
const short BUT_POS_X[N][3] = { { 560, 756, 980 }, {560, 756, 980}, {560, 756, 980}, {560, 756, 980} };
const short BUT_POS_Y[N][3] = { { 200, 256, 342 }, {279, 305, 441}, {358, 394, 540}, {437, 483, 639} };
// States of buttons
const short BUT_STATE[N][3] = {{0, 75, 153}, {0, 75, 153}, {0, 75, 153}, {0, 75, 153}};
const short BUT_SIZE[N][2] = {{140, 49}, {140, 49}, {140, 49}, {140, 49}};
// If button is difficult (for example one has more than one active zone), then one element of following array will be more than 0, else - 0
const short DIF_BUT[N] = {0, 0, 0, 0};
// Coordinates of labels
const short LAB_POS_X[M][3] = {{86, 198, 326}};
const short LAB_POS_Y[M][3] = {{102, 186, 314}};
// Labels size
const short LAB_SIZE[M][2] = {{628, 396}};
// If element of LAB_ANIM is true, then this texture is animation
const bool LAB_ANIM[M] = {true};
// Image length
const short LAB_IMG[1] = {41};
// Background color (Red, blue, green, alpha)
short BG_Color[4] = {0, 0, 0, 0};
// Thumb marker
const bool IS_TH[M] = {false};
// Menu pictures ways
string WAYS[] = { "Resurses\\MENU_Y.png\0",
"Resurses\\BUT_NEW.png\0",
"Resurses\\BUT_LOAD.png\0",
"Resurses\\BUT_SET.png\0",
"Resurses\\BUT_EXIT.png\0" }; // [5]
namespace load
{
//
}
namespace settings
{
const short N = 5; // Number of buttons
const short M = 3; // Number of labels
bool FULL_SC = false;
short cur_sr, cur_vol;
// "Screen resolution" - scroll, "Sound volume" - scroll, "Full screen" - check box,
// "Ok" - button, "Cancel" - button
const short BUT_POS_X[][3] = { {500, 696, 920}, {500, 696, 920}, {500, 696, 920}, {100, 100, 100}, {290, 304, 324} }; // [N][3]
const short BUT_POS_Y[][3] = { {100, 128, 170}, {139, 173, 223}, {178, 218, 276}, {451, 580, 797}, {451, 580, 797} }; // [N][3]
// States of buttons
const short BUT_STATE[][3] = {{0, 39, 78}, {0, 39, 78}, {0, 29, 58}, {0, 75, 153}, {0, 75, 153}}; // [N][3]
const short BUT_SIZE[][2] = {{200, 19}, {200, 19}, {200, 28}, {140, 49}, {140, 49}}; // [N][2]
const short DIF_BUT[] = {2, 2, 0, 0, 0}; // [N]
// X-coordinates of active zones boundaries.
// The First dimension is button; the Second dimension is active zone; the Third dimension contains coordinates of current zone
const short BUT_AREAS[][2][2] = {{{0, 20}, {180, 199}}, {{0, 20}, {180, 199}}}; // [2][2][2]
// Image segments for each active zone. The First dimension is button; the Second dimension is active zone;
const short BUT_SEGMENT[][2] = {{351, 0}, {117, 0}}; // [2][2]
// Special array. It's nessesary for screen resolution button.
const short BUT_ADVANCED[] = {117, 0}; // [2]
// Coordinates of labels
const short LAB_POS_X[][3] = {{86, 198, 326}, {250, 432, 640}, {250, 432, 640}, {500, 969, 920}}; // [M][3]
const short LAB_POS_Y[][3] = {{102, 186, 314}, {100, 128, 170}, {139, 173, 223}, {100, 128, 170}}; // [M][3]
// Labels size
const short LAB_SIZE[][2] = {{628, 396}, {200, 30}, {200, 30}, {20, 7}}; // [M][2]
// If element of LAB_ANIM is true, then this texture is animation
const bool LAB_ANIM[] = {true, false, false, false}; //[M]
// Image length
const short LAB_IMG[] = {41}; //[1]
// Thumb marker
const bool IS_TH[] = {false, false, false, true}; // [M]
// Positions of sound volume ("Sound thumb" - thumb)
const short POS_TH_X[][13] = {19, 25, 31, 37, 43, 49, 55, 61, 67, 73, 79, 85, 91}; // [1][13]
const short POS_TH_Y[][13] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}; // [1][13]
// Settings menu pictures ways
string WAYS[] = { "Resurses\\Sett\\MENU_Y.png\0",
"Resurses\\Sett\\SCR_RES.png\0",
"Resurses\\Sett\\SCR_RES.png\0",
"Resurses\\Sett\\THUMB.png\0",
"Resurses\\Sett\\THUMB.png\0",
"Resurses\\Sett\\BUT_OK.png\0",
"Resurses\\Sett\\BUT_CANC.png\0",
"Resurses\\Sett\\THUMB_fl.png\0",
"Resurses\\Sett\\TEXTS_V.png\0",
"Resurses\\Sett\\TEXTS_S.png\0" }; //[10]
}
}
namespace game
{
//
}
// string WAYS[5] = {"Resurses\\First\\BG.png\0", "Resurses\\First\\PLAYER.png\0"};
Заголовочный файл 'levels.h', где объявлен класс уровней
#pragma once
#ifndef _LEVEL_
#define _LEVEL_
#include "stdafx.h"
#include "global_variables.h"
class levels
{
public:
levels(SDL_Renderer*, short, short[]);
SDL_Renderer* level_processing(SDL_Renderer*, float, SDL_Rect[], short);
~levels();
private:
// n - number of textures; ANIM - animation counter
short n, ANIM, n_n, m_m;
SDL_Renderer* BACKGR = nullptr; // Background render
SDL_Renderer* _ren = nullptr; // Main renderer
SDL_Rect rectBG; // Rect structure for background
SDL_Rect *IMG = nullptr, *SUR = nullptr;
char way[255];
float bg[4];
// _SUR_A - Rect structure, which contain coordinates of active buttons
// _SUR_L - Rect structure, which contain coordinates of labels
// _IMG_A - Rect structure, which contain coordinates of position on image for active button
// _IMG_L - Rect structure, which contain coordinates of position on image for labels
// SIZE_A - This array contain states;
// N_ANIM - Array, which contain numbers of frames;
// ANIM_X - This array defines type label (Is it animation or not);
SDL_Rect *_IMG_A = nullptr, *_SUR_A = nullptr;
SDL_Rect *_IMG_L = nullptr, *_SUR_L = nullptr;
short *N_ANIM = nullptr;
bool *ANIM_X = nullptr;
SDL_Surface* SURF = nullptr;
SDL_Texture* TEXT = nullptr;
void redraw_textures();
};
#endif // _LEVEL_
Файл описания класса уровней
#include "levels.h"
levels::levels(SDL_Renderer* rMain, short _num_lvl, short ident[])
{
switch(_num_lvl)
{
case 1:
{using namespace menu;}
case 2:
{using namespace menu::load;}
case 3:
{using namespace menu::settings;}
case 4:
{using namespace game;}
}
short count = 0;
n_n = N;
m_m = M; ANIM = 0;
_ren = rMain;
TEXT = new SDL_Texture[N];
_SUR_A = new SDL_Rect[N]; _SUR_L = new SDL_Rect[M];
_IMG_A = new _SUR_A[N]; _IMG_L = new _SUR_A[M];
ANIM_X = new bool[M];
for(int i = 0; i < M; i++)
ANIM_X[i] = LAB_ANIM[i];
short G = 0;
for(int i = 0; i < M; i++)
if(LAB_ANIM[i])
G++;
N_ANIM = new short[G];
for(int i = 0; i < G; i++)
N_ANIM[i] = LAB_IMG[i];
for(int i = 0; i < N; i++)
{
_SUR_A[i].x = BUT_POS_X[i][nom_scr]; _SUR_A[i].y = BUT_POS_Y[i][nom_scr];
_SUR_A[i].w = BUT_SIZE[i][0]; _SUR_A[i].h = BUT_SIZE[i][1];
_IMG_A[i].x = 0; _IMG_A[i].y = BUT_STATE[i][0] + ident[i];
_IMG_A[i].w = BUT_SIZE[i][0]; _IMG_A.h = BUT_SIZE[i][1];
}
for(int i = 0; i < M; i++)
{
if(IS_TH[i])
{
_SUR_L[i].x = LAB_POS_X[i][nom_scr] + POS_TH_X[count][sound_vol];
_SUR_L[i].y = LAB_POS_X[i][nom_scr] + POS_TH_Y[count][sound_vol];
count++;
}
else
{
_SUR_L[i].x = LAB_POS_X[i][nom_scr];
_SUR_L[i].y = LAB_POS_X[i][nom_scr];
}
_SUR_L[i].w = LAB_SIZE[i][0];
_SUR_L[i].h = LAB_SIZE[i][1];
_IMG_L[i].x = 0; _IMG_L[i].y = 0;
_IMG_L[i].w = BUT_SIZE[i][0]; _IMG_L.h = BUT_SIZE[i][1];
}
for(int i = 0; i < 4; i++)
bg[i] = BG_Color[i];
for(int i = 0; i < n_n + m_m; i++)
{
std::memmove(way, WAYS[i].c_str(), WAYS[i].length());
std::memmove(way + WAYS[i].length(), "\0", 1);
SURF = IMG_Load(way);
TEXT[i] = SDL_CreateTextureFromSurface(_ren, SURF);
SDL_FreeSurface(SURF);
SURF = NULL;
}
}
SDL_Renderer* levels::level_processing(SDL_Renderer* rMain, float I, SDL_Rect _IMG[], short _num_lvl)
{
// rMain is render, in which will be copied textures
// I is variable for animation
// _IMG is array, which contain coordinates on image. It's necessary for buttons
switch(_num_lvl)
{
case 1:
{using namespace menu;}
case 2:
{using namespace menu::load;}
case 3:
{using namespace menu::settings;}
case 4:
{using namespace game;}
}
_ren = rMain;
short count = 0;
// Thumb settings
for(int i = 0; i < m_m; i++)
{
if(IS_TH[i])
{
_SUR_L[i].x = LAB_POS_X[i][nom_scr] + POS_TH_X[count][sound_vol];
_SUR_L[i].y = LAB_POS_X[i][nom_scr] + POS_TH_Y[count][sound_vol];
count++;
}
}
// Animation settings
count = 0;
int J = int(I);
for(int i = 0; i < M; i++)
if(ANIM_X[i])
{
while (J >= LAB_IMG[count])
J -= LAB_IMG[count];
_IMG_L[i].y = J*LAB_SIZE[i][1];
count++;
}
for(int i = 0; i < n_n; i++)
_IMG_A[i].y = _IMG[i].y;
redraw_textures();
return _ren;
}
void levels::redraw_textures()
{
SDL_RenderClear(_ren);
SDL_SetRenderDrawColor(BACKGR, bg[0], bg[1], bg[2], bg[3]);
SDL_RenderFillRect(BACKGR, &rectBG);
for(int i = 0; i < n_n; i++)
SDL_RenderCopy(_ren, TEXT[i], &_IMG_A[i], &_SUR_A[i]);
for(int i = n_n; i < m_m; i++)
SDL_RenderCopy(_ren, TEXT[i], &_IMG_L[i], &_SUR_L[i]);
SDL_RenderPresent(_ren);
}
levels::~levels()
{
for(int i = 0; i < n_n + m_m; i++)
SDL_DestroyTexture(TEXT[i]);
delete[] TEXT;
TEXT = NULL;
delete _ren;
_ren = NULL;
delete[] IMG;
IMG = NULL;
delete[] SURF;
SURF = NULL;
delete SURF;
SURF = NULL;
delete[] _IMG_A; delete[] _IMG_L;
delete[] _SUR_A; delete[] _SUR_L;
for(int i = 0; i < n_n; i++)
delete[] SIZE_A[i];
delete[] SIZE_A;
SIZE_A = NULL;
delete[] N_ANIM;
N_ANIM = NULL;
delete[] ANIM_X;
ANIM_X = NULL;
}
В последнем листинге я пытался реализовать выбор пространства имен с помощью switch оператора.