summaryrefslogtreecommitdiff
path: root/lib/base.cc
blob: acfc654e6f889537fbcb0f189c14fece1da23d23 (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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#include <stdio.h>
#include <SDL.h>
#include "base.h"
#include "engine.h"
#include "generic.h"
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include "gettext.h"

mogltk::base::base(int w, int h, int flags) throw(GeneralException) : surface(0) {
    width = w;
    height = h;
    
    mogltk::engine::setup();
    if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0) {
        throw GeneralException(String("Couldn't initialise Video SubSystem: ") + SDL_GetError());
    }

    if (!(surface = SDL_SetVideoMode(width, height, 0, flags | SDL_HWSURFACE))) {
        throw GeneralException(String("Couldn't set SDL mode: ") + SDL_GetError());
    }

    mogltk::engine::base_o = this;

    float ratio = surface->w;
    ratio /= surface->h;

    printm(M_INFO, "Video resolution: %dx%dx%d (ratio = %3.2f)\n", surface->w, surface->h, surface->format->BitsPerPixel, ratio);

    SDL_ShowCursor(SDL_DISABLE);
    SDL_FillRect(surface, NULL, 0);
    SDL_Flip(surface);
    SDL_FillRect(surface, NULL, 0);
    mogltk::engine::postsetup();
}

mogltk::base::~base() {
}

int mogltk::base::GetWidth(void) {
    return width;
}

int mogltk::base::GetHeight(void) {
    return height;
}

void mogltk::base::Flip() {
    mogltk::engine::pollevents();
    SDL_Flip(surface);
    SDL_FillRect(surface, NULL, 0);
}

mogltk::base::base(int w, int h, int flags, int) : surface(0) {
    width = w;
    height = h;

    SDL_ShowCursor(SDL_DISABLE);
}

void mogltk::base::setsurface(SDL_Surface * _surface) throw (GeneralException) {
    if (surface)
	throw GeneralException("Can't set video surface twice");
    surface = _surface;
}

SDL_Surface * mogltk::base::getsurface() {
    return surface;
}

void mogltk::base::Enter2DMode() {
}

void mogltk::base::Leave2DMode() {
}

bool mogltk::base::is2D() {
    return true;
}

void mogltk::base::changeviewport(int x, int y, unsigned int w, unsigned int h) {
    SDL_Rect r;

    if ((w == 0) && (h == 0)) {
	w = GetWidth() - x;
	h = GetHeight() - y;
    }

    r.x = x;
    r.y = y;
    r.w = w;
    r.h = h;

    SDL_SetClipRect(surface, &r);
}

void mogltk::base::ToggleFullscreen() {
    int newflags = surface->flags;

    if (surface->flags & SDL_FULLSCREEN)
        newflags &= ~SDL_FULLSCREEN;
    else
        newflags |= SDL_FULLSCREEN;

    texture::Taintall();

    surface = SDL_SetVideoMode(surface->w, surface->h, surface->format->BitsPerPixel,
        SDL_HWSURFACE | newflags);
}