college/ws2010/gdi3/p5/Painter.class.cpp
2011-10-18 10:28:57 +02:00

119 lines
2.4 KiB
C++

#include "Painter.class.h"
void CPainter::print(int outtype, int** outarray, int x_width, int y_height){
switch(outtype){
case 1:
CPainter::printASCIIMandelbrot(outarray, x_width, y_height);
break;
case 2:
CPainter::printPIXELMandelbrot(outarray, x_width, y_height);
break;
default:
printf("Keine Ausgabe!\n");
}
}
void CPainter::printASCIIMandelbrot(int** outarray, int x_width, int y_height){
for(int i=0;i<y_height;i++){
for(int j=0;j<x_width;j++){
if(outarray[j][i]==0){
printf(" ");
} else {
printf("X");
}
}
printf("\n");
}
}
void CPainter::printPIXELMandelbrot(int** outarray, int x_width, int y_height){
SDL_Delay(16);
SDL_Init(SDL_INIT_VIDEO);
SDL_Surface* screen = SDL_SetVideoMode(x_width, y_height, 0, SDL_HWPALETTE);
SDL_WM_SetCaption("my Mandelbrot", "my Mandelbrot");
SDL_Event _event;
for(int i=0;i<y_height;i++){
for(int j=0;j<x_width;j++){
if(outarray[j][i]==0){
CPainter::DrawPixel(screen,j,i,0,0,0);
} else {
CPainter::DrawPixel(screen,j,i,255,255,255);
}
}
}
SDL_UpdateRect(screen, x_width, y_height, 1, 1);
bool done=false;
while(!done) {
while(SDL_PollEvent(&_event)) {
if (_event.type == SDL_QUIT) {
done=true;
}
}
}
SDL_FreeSurface;
SDL_Quit();
}
void CPainter::DrawPixel(SDL_Surface *screen, int x, int y, Uint8 R, Uint8 G, Uint8 B){
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
if ( SDL_MUSTLOCK(screen) ) {
if ( SDL_LockSurface(screen) < 0 ) {
return;
}
}
switch (screen->format->BytesPerPixel) {
case 1: { /* vermutlich 8 Bit */
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x;
*bufp = color;
}
break;
case 2: { /* vermutlich 15 Bit oder 16 Bit */
Uint16 *bufp;
bufp = (Uint16 *)screen->pixels + y*screen->pitch/2 + x;
*bufp = color;
}
break;
case 3: { /* langsamer 24-Bit-Modus, selten verwendet */
Uint8 *bufp;
bufp = (Uint8 *)screen->pixels + y*screen->pitch + x * 3;
if(SDL_BYTEORDER == SDL_LIL_ENDIAN) {
bufp[0] = color;
bufp[1] = color >> 8;
bufp[2] = color >> 16;
} else {
bufp[2] = color;
bufp[1] = color >> 8;
bufp[0] = color >> 16;
}
}
break;
case 4: { /* vermutlich 32 Bit */
Uint32 *bufp;
bufp = (Uint32 *)screen->pixels + y*screen->pitch/4 + x;
*bufp = color;
}
break;
}
if ( SDL_MUSTLOCK(screen) ) {
SDL_UnlockSurface(screen);
}
}