OpenCores
URL https://opencores.org/ocsvn/s80186/s80186/trunk

Subversion Repositories s80186

[/] [s80186/] [trunk/] [sim/] [display/] [Window.h] - Blame information for rev 2

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 2 jamieiles
// Copyright Jamie Iles, 2017
2
//
3
// This file is part of s80x86.
4
//
5
// s80x86 is free software: you can redistribute it and/or modify
6
// it under the terms of the GNU General Public License as published by
7
// the Free Software Foundation, either version 3 of the License, or
8
// (at your option) any later version.
9
//
10
// s80x86 is distributed in the hope that it will be useful,
11
// but WITHOUT ANY WARRANTY; without even the implied warranty of
12
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
// GNU General Public License for more details.
14
//
15
// You should have received a copy of the GNU General Public License
16
// along with s80x86.  If not, see <http://www.gnu.org/licenses/>.
17
 
18
#pragma once
19
 
20
#include <SDL.h>
21
#include <stdexcept>
22
 
23
class Window
24
{
25
public:
26
    Window(const char *name, int width, int height)
27
        : width(width), height(height)
28
    {
29
        compute_scale();
30
 
31
        auto ret = SDL_CreateWindowAndRenderer(window_width, window_height,
32
                                               SDL_WINDOWPOS_UNDEFINED, &window,
33
                                               &renderer);
34
        if (ret)
35
            throw std::runtime_error("unable to create window");
36
 
37
        SDL_SetWindowTitle(window, name);
38
        SDL_RenderSetScale(renderer, x_scale_factor, y_scale_factor);
39
    }
40
 
41
    ~Window()
42
    {
43
        SDL_DestroyWindow(window);
44
        SDL_DestroyRenderer(renderer);
45
    }
46
 
47
    SDL_Surface *surface()
48
    {
49
        return SDL_GetWindowSurface(window);
50
    }
51
 
52
    SDL_Window *get()
53
    {
54
        return window;
55
    }
56
 
57
    void clear()
58
    {
59
        SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0);
60
        SDL_RenderClear(renderer);
61
    }
62
 
63
    void set_pixel(int row,
64
                   int col,
65
                   unsigned char r,
66
                   unsigned char g,
67
                   unsigned char b)
68
    {
69
        SDL_SetRenderDrawColor(renderer, r, g, b, 255);
70
        SDL_RenderDrawPoint(renderer, row, col);
71
    }
72
 
73
    void redraw()
74
    {
75
        SDL_RenderPresent(renderer);
76
    }
77
 
78
    int get_width() const
79
    {
80
        return width;
81
    }
82
 
83
    int get_height() const
84
    {
85
        return height;
86
    }
87
 
88
private:
89
    void compute_scale()
90
    {
91
        SDL_DisplayMode display_mode;
92
        SDL_GetCurrentDisplayMode(0, &display_mode);
93
 
94
        window_width = display_mode.w / 2;
95
        window_height = ((float)height / (float)width) * window_width * 2;
96
 
97
        x_scale_factor = window_width / (float)width;
98
        y_scale_factor = window_height / (float)height;
99
    }
100
 
101
    int width;
102
    int height;
103
    SDL_Window *window;
104
    SDL_Renderer *renderer;
105
    float x_scale_factor;
106
    float y_scale_factor;
107
    int window_width;
108
    int window_height;
109
};

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.