-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScreen.cpp
57 lines (44 loc) · 1.46 KB
/
Screen.cpp
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
//
// Screen.cpp
// Chip-8 Emu
//
// Created by Daniel Hauser on 19.12.14.
// Copyright (c) 2014 Daniel Hauser. All rights reserved.
//
#include "Screen.hpp"
#include "Preferences.hpp"
namespace UI
{
Screen::Screen(const std::string &title, int width, int height,
int texWidth, int texHeight)
: mTexWidth(texWidth), mTexHeight(texHeight)
{
mSuccess = false;
mWindow = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
width, height, Preferences::Resizable() ? SDL_WINDOW_RESIZABLE : 0);
if(!mWindow) return;
mRenderer = SDL_CreateRenderer(mWindow, -1, SDL_RENDERER_ACCELERATED);
if(!mRenderer) return;
mTexture = SDL_CreateTexture(mRenderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING,
texWidth, texHeight);
if(!mTexture) return;
mSuccess = true;
}
Screen::~Screen()
{
if(mTexture) SDL_DestroyTexture(mTexture);
if(mRenderer) SDL_DestroyRenderer(mRenderer);
if(mWindow) SDL_DestroyWindow(mWindow);
}
void Screen::Draw(const uint8_t *vram)
{
uint32_t pixels[mTexWidth * mTexHeight];
for(int i = 0; i < mTexWidth * mTexHeight; ++i)
{
pixels[i] = vram[i] ? mDrawColor : mClearColor;
}
SDL_UpdateTexture(mTexture, nullptr, pixels, 4 * mTexWidth);
SDL_RenderCopy(mRenderer, mTexture, nullptr, nullptr);
SDL_RenderPresent(mRenderer);
}
}