Handmade Hero » Forums » Code » Trying to do a function to create the main window
unfear
6 posts
#12334 Trying to do a function to create the main window
3 months, 3 weeks ago Edited by on July 3, 2017, midnight

Hi, I try to compact more the code to functions, can I do a unique function to call a window? but, I don't know if this is a good idea, can someone help me and explain?

Very thanks :)


  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
#include <windows.h>
#include <stdint.h>

/*---------------------------------------------------------------
	Anotacoes
---------------------------------------------------------------*/
/*
	Notas
*/

/*---------------------------------------------------------------
	Aliases
---------------------------------------------------------------*/
//Alias para diferenciar os diferentes tipos de static
#define static_interna static //funcoes que apenas podem ser visiveis para as outras funcoes no mesmo arquivo 
#define static_local static //apenas dentro do mesmo escopo
#define static_global static //variaveis globais 

//Alias para outras variaveis
typedef int8_t int8;
typedef int16_t int16;
typedef int32_t int32;
typedef int64_t int64;
typedef uint8_t uint8;
typedef uint16_t uint16;
typedef uint32_t uint32;
typedef uint64_t uint64;
typedef int32 bool32;


/*---------------------------------------------------------------
	Estruturas
---------------------------------------------------------------*/



/*---------------------------------------------------------------
	Variaveis
---------------------------------------------------------------*/
static_global bool GlobalExecutar;


/*---------------------------------------------------------------
	Funcoes
---------------------------------------------------------------*/
LRESULT CALLBACK Win32JanelaCallback(HWND Window, UINT Message, WPARAM wParam, LPARAM lParam){
	LRESULT Result = 0;

	switch(Message) {
		case WM_CLOSE:{
			GlobalExecutar= false;
		} break;
		case WM_DESTROY:{
			GlobalExecutar= false;
		} break;
		case WM_SIZE:{
			OutputDebugStringA("WM_SIZE\n");
		} break;
		case WM_ACTIVATEAPP:{
		} break;

		default:{
			//Para garantir que toda MSG sera processada
			Result= DefWindowProc(Window, Message, wParam, lParam);
		} break;
	}
	return(Result);
}

static_interna void Win32RetornoJanela(){
	MSG Message;
	while(PeekMessage(&Message, 0, 0, 0, PM_REMOVE)){
		if(Message.message == WM_QUIT){
			GlobalExecutar= false;
		}
		//Traduz uma MSG para o caracter WM_CHAR
		TranslateMessage(&Message);
		//Envia a MSG para o procedimento da janela
		DispatchMessage(&Message);
	}
}


static_interna bool Win32CriarJanela(HINSTANCE &Instancia){

	//Estrutura WNDCLASS com as variaveis para criar a janela
	WNDCLASS EstruturaJanela = {};
	EstruturaJanela.style = CS_HREDRAW|CS_VREDRAW|CS_OWNDC;
	EstruturaJanela.lpfnWndProc = Win32JanelaCallback;
	EstruturaJanela.hInstance = Instancia;
	//EstruturaJanela.hIcon;
	EstruturaJanela.lpszClassName= "JanelaPrincipal";

	//Registrar a estrutura da janela com as variaveis da estrutura WNDCLASS
	if(RegisterClass(&EstruturaJanela)){

		//Criar a janela
		if(CreateWindowEx(
			0,
			EstruturaJanela.lpszClassName,
			"Unfear",
			WS_OVERLAPPEDWINDOW|WS_VISIBLE,
			CW_USEDEFAULT,
			CW_USEDEFAULT,
			CW_USEDEFAULT,
			CW_USEDEFAULT,
			0,
			0,
			Instancia,
			0
		)){
			return(1);
		}
	}
	return(0);
}


/*---------------------------------------------------------------
	Escopo do codigo principal do windows (MAIN)
---------------------------------------------------------------*/
int CALLBACK WinMain(HINSTANCE Instancia, HINSTANCE InstanciaAnterior, LPSTR LinhaDeComando, int ExibirCodigo){
	if(Win32CriarJanela(Instancia)){
		GlobalExecutar = true;
		while(GlobalExecutar){
			Win32RetornoJanela();
		}
	}
	return(0);
}
mmozeiko
Mārtiņš Možeiko
1485 posts
1 project
#12335 Trying to do a function to create the main window
3 months, 3 weeks ago

This is perfectly fine. Structuring code in a way that some relatively isolated functionality is extracted to functions is OK thing to do.