summaryrefslogtreecommitdiff
path: root/src/graphic.c
blob: dbdb55cd7d791e222d19d708a796947564a76e41 (plain)
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
#include "graphic.h"
#include "utils.h"

void my_SDL_init_or_die(char title[], SDL_Rect win_pos, Uint32 init_flags, Uint32 win_flags, Uint32 rend_flags, SDL_Window **win, SDL_Renderer **rend, SDL_RendererInfo *rend_info, SDL_Rect *viewport)
{
	int res, i;
	Uint32 tf;

	res=SDL_Init(init_flags);
	MPL_CHECK(
			res==0,	// Expression to evaluate
			{exit(1);} , // Code to execute if expression is not true
			SDL_LOG_PRIORITY_CRITICAL, // SDL_LogPriority (_CRITICAL, _ERROR, _WARN, _INFO, DEBUG, _VERBOSE)
			"SDL_Init failed (%i)",res // var args list starting with a fmt string like in printf()
		 );


	*win=SDL_CreateWindow(title,win_pos.x,win_pos.y,win_pos.w,win_pos.h,win_flags);
	MPL_CHECK(
			*win, // Just put the pointer if you want to check if it's not NULL
			{SDL_Quit(); exit(2);},
			SDL_LOG_PRIORITY_CRITICAL,
			"SDL_CreateWindow failed"
		 );


	SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_INFO,
			"SDL_GetWindowPixelFormat() returns %i", SDL_GetWindowPixelFormat(*win)
		      );

	*rend=SDL_CreateRenderer(*win,-1, rend_flags);
	MPL_CHECK(
			*rend,
			{SDL_DestroyWindow(*win);SDL_Quit(); exit(3);},
			SDL_LOG_PRIORITY_CRITICAL,
			"SDL_CreateRenderer(...,SDL_RENDERER_ACCELERATED) failed"
		 );

	res=SDL_GetRendererInfo(*rend, rend_info);
	MPL_CHECK(
			 res==0,
			 {SDL_DestroyRenderer(*rend);SDL_DestroyWindow(*win);SDL_Quit(); exit(4);},
			 SDL_LOG_PRIORITY_CRITICAL,
			 "res=SDL_GetRendererInfo() failed"
		 );

	tf=1;
	for(i=0;i<16 && tf!=0;i++) {
		tf=rend_info->texture_formats[i];

		SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_INFO,
				"rend_info->texture_formats[%i] == %i", i, tf
			      );
	}

	SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_INFO,
			"rend_info->flags==%i", rend_info->flags );

	if ( (rend_info->flags & SDL_RENDERER_SOFTWARE) == SDL_RENDERER_SOFTWARE) {
		SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_INFO,
				"rend_info->flags contains SDL_RENDERER_SOFTWARE");
	}

	if ( (rend_info->flags & SDL_RENDERER_ACCELERATED) == SDL_RENDERER_ACCELERATED) {
		SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_INFO,
				"rend_info->flags contains SDL_RENDERER_ACCELERATED");
	}

	if ( (rend_info->flags & SDL_RENDERER_PRESENTVSYNC) == SDL_RENDERER_PRESENTVSYNC) {
		SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_INFO,
				"rend_info->flags contains SDL_RENDERER_PRESENTVSYNC");
	}

	if ( (rend_info->flags & SDL_RENDERER_TARGETTEXTURE) == SDL_RENDERER_TARGETTEXTURE) {
		SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION,SDL_LOG_PRIORITY_INFO,
				"rend_info->flags contains SDL_RENDERER_TARGETTEXTURE");
	}

	// Remarks : When the window is resized, the current viewport is automatically centered within the new window size.
	SDL_RenderGetViewport(*rend, viewport);
}