Skip to content
Snippets Groups Projects
Verified Commit 1753a825 authored by João Magalhães's avatar João Magalhães :rocket:
Browse files

chore: improved performance of sdl2

parent b48963b2
No related branches found
No related tags found
1 merge request!36Support for Python
Pipeline #3646 passed
......@@ -223,7 +223,7 @@ This is a [Game Boy](https://en.wikipedia.org/wiki/Game_Boy) emulator built usin
self._video.save_frame(self.image(), self._frame_index)
self._video.compute_next(self._frame_index)
#@TODO: this should be sample, meaning that not every
# @TODO: this should be sample, meaning that not every
# single frame is sent to the display (performance)
if self._display != None:
from .graphics import Display
......
......@@ -18,6 +18,7 @@ class Display:
_height: int = DISPLAY_HEIGHT
_title: str = "Boytacean"
_window: Union[Window, None] = None
_renderer: Union[Renderer, None] = None
def __init__(
self,
......@@ -29,25 +30,27 @@ class Display:
self._height = height
self._title = title
self._window = None
self._renderer = None
self.build()
def build(self):
init_sdl()
self._window = Window(self._title, size=(self._width, self._height))
self._window.show()
self._renderer = Renderer(self._window)
def render_frame(self, frame_buffer: bytes):
if not self._window:
raise RuntimeError("Window not initialized")
if not self._renderer:
raise RuntimeError("Renderer not initialized")
# we consider that every time there's a request for a new
# frame draw the queue of SDL events should be flushed
events = get_events()
for event in events:
if event.type == SDL_QUIT:
running = False # @TODO need to destroy the engine
break
# @TODO: this should not be build every single frame
renderer = Renderer(self._window)
pass
surface = SDL_CreateRGBSurfaceFrom(
frame_buffer,
......@@ -55,23 +58,23 @@ class Display:
DISPLAY_HEIGHT,
24,
DISPLAY_WIDTH * 3,
0x000000FF,
0x0000FF00,
0x00FF0000,
0x000000ff,
0x0000ff00,
0x00ff0000,
0x0,
)
texture = SDL_CreateTextureFromSurface(renderer.sdlrenderer, surface.contents)
# @TODO: not sure this clear is required
renderer.clear(Color(0, 0, 0))
SDL_RenderCopy(
renderer.sdlrenderer,
texture,
None,
SDL_Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
texture = SDL_CreateTextureFromSurface(
self._renderer.sdlrenderer, surface.contents
)
renderer.present()
# @TODO: need to destroy latter on
# SDL_DestroyTexture(texture)
# SDL_FreeSurface(surface)
try:
SDL_RenderCopy(
self._renderer.sdlrenderer,
texture,
None,
SDL_Rect(0, 0, DISPLAY_WIDTH, DISPLAY_HEIGHT),
)
self._renderer.present()
finally:
SDL_DestroyTexture(texture)
SDL_FreeSurface(surface)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment