summaryrefslogtreecommitdiffstats
path: root/mpeglib/lib/util/render/sdl/sdlSurface.cpp
blob: 86ef41bb9fb6673d4371b76f520db24898011a0c (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/*
  surface wrapper for SDL
  Copyright (C) 2000  Martin Vogt

  This program is free software; you can redistribute it and/or modify
  it under the terms of the GNU Library General Public License as published by
  the Free Software Foundation.

  For more information look at the file COPYRIGHT in this package

 */



#include "sdlSurface.h"


#ifdef SDL_WRAPPER


SDLSurface::SDLSurface() {
  surface=NULL;
  lOpen=false;
  imageMode=_IMAGE_NONE;
  lSDLInit=false;
  imageDeskSDL=new ImageDeskSDL();
}


SDLSurface::~SDLSurface() {
  close();
  if (imageCurrent != NULL) {
    imageCurrent->closeImage();
    delete imageCurrent;
    // otherwise test of NULL will fail
    imageCurrent = NULL;
  }
}


int SDLSurface::isOpen() {
  return lOpen;
}

int SDLSurface::getImageMode() {
  return imageMode;
}

int SDLSurface::open(int width, int height,const char *title,bool border) {
  cout << "SDL openImage:"<<title<<endl;

  sdlinit();

  rect.x = 0;
  rect.y = 0;
  rect.w = width;
  rect.h = height;

  return true;
}


int SDLSurface::close() {
  if (isOpen()==false) {
    cout << "WindowOut::closeWindow already closed"<<endl;
    return true;
  }
  if(surface) {
    SDL_FreeSurface(surface);
    surface = NULL;
  }

  lOpen=false;
  return true;
}


int SDLSurface::getHeight() {
  return rect.h;
}


int SDLSurface::getWidth() {
  return rect.w;
}


int SDLSurface::getDepth() {
  return video_bpp;
}

int SDLSurface::putImage(YUVPicture* ) {
  return true;
}


int SDLSurface::openImage(int imageMode, YUVPicture* pic) {
  if (this->imageMode != _IMAGE_NONE) {
    cout << "bad open error X11Surface::openImage"<<endl;
    exit(0);
  }
  cout << "************* openImage SDL"<<imageMode<<endl;
  this->imageMode=imageMode;
  imageCurrent=NULL;
  int w=getWidth();
  int h=getHeight();
  if(imageMode & _IMAGE_RESIZE) {
    w=resize_rect.w; 
    h=resize_rect.h; 
  }
 
  if (imageMode & _IMAGE_DOUBLE) {
    w=rect.w*2;
    h=rect.h*2;
  }

  if (imageMode & _IMAGE_DESK) {
    if (imageDeskSDL->support()) {
      imageCurrent=imageDeskSDL;
      int video_flags = SDL_SWSURFACE;
      video_flags |= SDL_ASYNCBLIT;
      video_flags |= SDL_RESIZABLE;
      if(surface)
	SDL_FreeSurface(surface);
      surface = SDL_SetVideoMode(w, h, video_bpp, video_flags);

    }
  }
  if (imageMode & _IMAGE_FULL) {
    if (imageDeskSDL->support()) {
      imageCurrent=imageDeskSDL;
      int video_flags = SDL_FULLSCREEN;
      video_flags |= SDL_ASYNCBLIT;
      video_flags |= SDL_HWSURFACE;
      if(surface)
	SDL_FreeSurface(surface);
      surface = SDL_SetVideoMode(w, h, video_bpp, video_flags);
    }
  }
  if (imageCurrent != NULL) {
    cout << "surface:"<<surface<<endl;
    imageCurrent->init((XWindow*)surface,(YUVPicture*)&rect);
    imageCurrent->openImage(imageMode);
  }
  return (imageCurrent != NULL);
}


int SDLSurface::closeImage() {
  this->imageMode = _IMAGE_NONE;
  if (imageCurrent != NULL) {
    imageCurrent->closeImage();
  }
  imageCurrent=NULL;
  return true;
}

int SDLSurface::checkEvent(int* newMode) {
  int back=false;
  SDL_Event event;
  SDL_PollEvent(&event);
  switch (event.type) {
  case SDL_MOUSEBUTTONDOWN: {
    int button=event.button.button;
    if (button == 1) {
      *newMode = imageMode ^ _IMAGE_DOUBLE;
      back=true;
    }
    if (button == 3) {
      *newMode = imageMode ^ _IMAGE_DESK ^ _IMAGE_FULL;
      back=true;
    }
    break;
  }
  case SDL_VIDEORESIZE : {
    resize_rect.w = event.resize.w;
    resize_rect.h = event.resize.h;
    *newMode = imageMode | _IMAGE_RESIZE;   
    back = true;
    break;
  }

  }
  return back;
}

int SDLSurface::dither(YUVPicture* pic) {
  if (imageCurrent != NULL) {
    imageCurrent->ditherImage(pic);
    if(imageMode & _IMAGE_RESIZE) {
      imageCurrent->putImage(resize_rect.w, resize_rect.h);
    } else {
      imageCurrent->putImage(rect.w, rect.h);
    }
  }
  return true;
}

void SDLSurface::sdlinit() {
  if (lSDLInit == false) {
    if (SDL_Init(SDL_INIT_VIDEO) < 0 ) {
      fprintf(stderr, "Warning: Couldn't init SDL video: %s\n",
	      SDL_GetError());
      fprintf(stderr, "Will ignore video stream\n");
      exit(0);
    }
    atexit(SDL_Quit);
    cout << "****************** SDL VIDEO System **********"<<endl;
    /* Get the "native" video mode */
    video_info=SDL_GetVideoInfo();
    video_bpp=video_info->vfmt->BitsPerPixel;
    imageMode=_IMAGE_NONE;
    
    imageCurrent=NULL; 
  }
  lSDLInit=true;
}

#endif