all repos — mgba @ f6755a6e1b7b0cf2b944cd8ca842746f11d6bf82

mGBA Game Boy Advance Emulator

src/platform/sdl/rpi-common.c (view raw)

 1/* Copyright (c) 2013-2015 Jeffrey Pfau
 2 *
 3 * This Source Code Form is subject to the terms of the Mozilla Public
 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 6#include "main.h"
 7
 8#include <mgba/core/version.h>
 9
10void mRPIGLCommonSwap(struct VideoBackend* context) {
11	struct mSDLRenderer* renderer = (struct mSDLRenderer*) context->user;
12	eglSwapBuffers(renderer->eglDisplay, renderer->eglSurface);
13}
14
15void mRPIGLCommonInit(struct mSDLRenderer* renderer) {
16	bcm_host_init();
17	renderer->eglDisplay = eglGetDisplay(EGL_DEFAULT_DISPLAY);
18	int major, minor;
19	if (EGL_FALSE == eglInitialize(renderer->eglDisplay, &major, &minor)) {
20		printf("Failed to initialize EGL");
21		return false;
22	}
23
24	if (EGL_FALSE == eglBindAPI(EGL_OPENGL_ES_API)) {
25		printf("Failed to get GLES API");
26		return false;
27	}
28
29	const EGLint requestConfig[] = {
30		EGL_RED_SIZE, 5,
31		EGL_GREEN_SIZE, 5,
32		EGL_BLUE_SIZE, 5,
33		EGL_ALPHA_SIZE, 1,
34		EGL_SURFACE_TYPE, EGL_WINDOW_BIT,
35		EGL_NONE
36	};
37
38	EGLConfig config;
39	EGLint numConfigs;
40
41	if (EGL_FALSE == eglChooseConfig(renderer->eglDisplay, requestConfig, &config, 1, &numConfigs)) {
42		printf("Failed to choose EGL config\n");
43		return false;
44	}
45
46	const EGLint contextAttributes[] = {
47		EGL_CONTEXT_CLIENT_VERSION, 2,
48		EGL_NONE
49	};
50
51	int dispWidth = 240, dispHeight = 160, adjWidth;
52	renderer->eglContext = eglCreateContext(renderer->eglDisplay, config, EGL_NO_CONTEXT, contextAttributes);
53	graphics_get_display_size(0, &dispWidth, &dispHeight);
54	adjWidth = dispHeight / 2 * 3;
55
56	DISPMANX_DISPLAY_HANDLE_T display = vc_dispmanx_display_open(0);
57	DISPMANX_UPDATE_HANDLE_T update = vc_dispmanx_update_start(0);
58
59	VC_RECT_T destRect = {
60		.x = (dispWidth - adjWidth) / 2,
61		.y = 0,
62		.width = adjWidth,
63		.height = dispHeight
64	};
65
66	VC_RECT_T srcRect = {
67		.x = 0,
68		.y = 0,
69		.width = 240 << 16,
70		.height = 160 << 16
71	};
72
73	DISPMANX_ELEMENT_HANDLE_T element = vc_dispmanx_element_add(update, display, 0, &destRect, 0, &srcRect, DISPMANX_PROTECTION_NONE, 0, 0, 0);
74	vc_dispmanx_update_submit_sync(update);
75
76	renderer->eglWindow.element = element;
77	renderer->eglWindow.width = dispWidth;
78	renderer->eglWindow.height = dispHeight;
79
80	renderer->eglSurface = eglCreateWindowSurface(renderer->eglDisplay, config, &renderer->eglWindow, 0);
81	if (EGL_FALSE == eglMakeCurrent(renderer->eglDisplay, renderer->eglSurface, renderer->eglSurface, renderer->eglContext)) {
82		return false;
83	}
84}