RawMessage.cpp 3.73 KB
Newer Older
1 2
/* 
 * File:   RawMessage.cpp
3
 * Author: Johannes Fischer
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
 * 
 * Created on July 14, 2014, 11:38 AM
 */

#include "RawMessage.h"

ColorConverter RawMessage::con;

RawMessage::RawMessage(int timestamp, Inflater* in, int size) {
    this->timestamp=timestamp;
    in->readShort(&x);
    in->readShort(&y);
    in->readShort(&w);
    in->readShort(&h);
    data=new SizedArray(size-8);
    in->readSizedArray(data);
}

RawMessage::~RawMessage() {
    delete data;
}

bool RawMessage::completeScreen(int w, int h){
    return (this->w==w && this->h==h);
}

void RawMessage::paint(SDL_Surface* screen, ProtocolPreferences* prefs){
    handleRawRect(screen,prefs);
32
    //printf("Drawn RawMessage at {%d, %d, %d, %d}\n",x,y,w,h);
33 34
}

35 36 37 38 39 40
/**
 * This method has been copied from the Java TTT-Player and adjusted to C++
 * It draws raw Pixels to the screen
 * @param screen
 * @param prefs
 */
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
void RawMessage::handleRawRect(SDL_Surface *screen, ProtocolPreferences* prefs)
{
    SDL_Rect rect = {0, 0, 1, 1};
    
    switch (prefs->bytesPerPixel) {
        case 1:
        {
            for (int dy = y; dy < y + h; dy++) {
                //is.readFully(graphicsContext.pixels8, dy * graphicsContext.prefs->framebufferWidth + x, w);
                read((char*)(screen->pixels)+dy * prefs->framebufferWidth + x,w);

                /*// buffering          TODO:?
                if (os != null)
                    os.write(graphicsContext.pixels8, dy * graphicsContext.prefs->framebufferWidth + x, w);*/
            }
            break;
        }
        case 2:
        {
            unsigned char rawColor[prefs->bytesPerPixel];
            uint color;
            if (prefs->bigEndian)
                for (int dy = y; dy < y + h; dy++)
                {
                    for(int dx = 0; dx < w; dx++)
                    {
                        read((char*)rawColor, prefs->bytesPerPixel);
                        color = con.decodeColor(rawColor,prefs->bytesPerPixel,prefs->format);
                        rect.x = dx + x;
                        rect.y = dy;
                        SDL_FillRect(screen, &rect, color);
                    }
                }
            else
                for (int dy = y; dy < y + h; dy++)
                {
                    for(int dx = 0; dx < w; dx++)
                    {
                        read((char*)rawColor, prefs->bytesPerPixel);
                        color = con.decodeColor(rawColor,prefs->bytesPerPixel,prefs->format);
                        rect.x = dx + x;
                        rect.y = dy;
                        SDL_FillRect(screen, &rect, color);
                    }
                }
            break;
        }
        default:
        {
            /*char buf[w * 4];
            for (int dy = y; dy < y + h; dy++) {
                read(buf,w*4);

                /*//* buffering
                if (os != null)
                    os.write(buf);*//*

                int offset = dy * prefs->framebufferWidth + x;
                if (prefs->bigEndian)
                    for (int i = 0; i < w; i++)
                        (int*)(screen->pixels)[offset + i] = (buf[i * 4 + 1] & 0xFF) << 16 | (buf[i * 4 + 2] & 0xFF) << 8 | (buf[i * 4 + 3] & 0xFF);
                else
                    for (int i = 0; i < w; i++)
                        (int*)(screen->pixels)[offset + i] = (buf[i * 4 + 2] & 0xFF) << 16 | (buf[i * 4 + 1] & 0xFF) << 8 | (buf[i * 4] & 0xFF);
            }*/
            break;
        }
    }
}

bool RawMessage::read(char* dest, int n){
    for(int i = 0; i < n; i++)
    {
        if(offSet>=data->length)
        {
            //printf("failed\n");
            return false;
        }
        dest[i]=data->array[offSet];
        offSet++;
    }
    return true;
}
124 125 126 127

int RawMessage::getArea() {
    return w*h;
}