IndexEntry.cpp 3.09 KB
Newer Older
daFischer's avatar
daFischer committed
1 2
/* 
 * File:   IndexEntry.cpp
3
 * Author: Johannes Fischer
daFischer's avatar
daFischer committed
4 5 6 7 8 9 10
 * 
 * Created on July 24, 2014, 12:54 AM
 */

#include <SDL/SDL_video.h>

#include "IndexEntry.h"
11
#include "ProtocolPreferences.h"
daFischer's avatar
daFischer committed
12 13 14 15 16

IndexEntry::IndexEntry(char* title, int timestamp, SizedArray* searchable, SDL_Surface* image) {
    this->title=title;
    this->timestamp=timestamp;
    this->searchable=searchable;
17
    this->thumbnail=image;
18
    waypoint=NULL;
19
    hasThumbnail=image!=NULL;
20 21 22 23 24
    hasImages=false;
}

void IndexEntry::setWaypoint(SDL_Surface* waypoint) {
    this->waypoint=waypoint;
25 26 27 28 29 30 31
    if(thumbnail==NULL)
    {
        SDL_LockSurface(waypoint);
        thumbnail=scaleDownSurface(waypoint, 5);
        SDL_UnlockSurface(waypoint);
        hasThumbnail=true;
    }
32 33 34 35 36 37
    hasImages=true;
}

void IndexEntry::paintWaypoint(SDL_Surface* screen) {
    SDL_Rect rect = {0,0,screen->w,screen->h};
    SDL_BlitSurface(waypoint,NULL,screen,&rect);
daFischer's avatar
daFischer committed
38 39 40
}

IndexEntry::~IndexEntry() {
41
    SDL_FreeSurface(thumbnail);
42 43 44 45
    SDL_FreeSurface(waypoint);
    if(title!=NULL)
        free(title);
    delete(searchable);
daFischer's avatar
daFischer committed
46 47 48
}

SDL_Rect IndexEntry::getRect(SDL_Surface* screen, int x, int y) {
49
    if(!hasThumbnail)
50 51 52 53
    {
        SDL_Rect r= {0,0,0,0};
        return r;
    }
54
    SDL_Rect r= {max(0,min(screen->w-thumbnail->w,x-thumbnail->w/2)),y-thumbnail->h,thumbnail->w,thumbnail->h};
daFischer's avatar
daFischer committed
55 56 57
    return r;
}

58
void IndexEntry::paintThumbnail(SDL_Surface* screen, int x, int y) {
59
    if(!hasThumbnail)
60
        return;
daFischer's avatar
daFischer committed
61
    SDL_Rect rect = getRect(screen,x,y);
62
    SDL_BlitSurface(thumbnail,NULL,screen,&rect);
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
    SDL_UpdateRect(screen, rect.x,rect.y,rect.w,rect.h);
}

Uint32 readPixel(SDL_Surface* source, int x, int y){
    switch(source->format->BytesPerPixel)
    {
        case 1:
            return ((unsigned char*)source->pixels)[x+source->w*y];
        case 2:
            return ((unsigned short*)source->pixels)[x+source->w*y];
        default:
            return ((unsigned int*)source->pixels)[x+source->w*y];
    }
}

void writePixel(SDL_Surface* target, int x, int y, Uint32 color){
    switch(target->format->BytesPerPixel)
    {
        case 1:
            ((unsigned char*)target->pixels)[x+target->w*y]=(unsigned char)color;
        case 2:
            ((unsigned short*)target->pixels)[x+target->w*y]=(unsigned short)color;
        default:
            ((unsigned int*)target->pixels)[x+target->w*y]=(unsigned int)color;
    }
}

SDL_Surface* IndexEntry::scaleDownSurface(SDL_Surface* source, char factor) {
    SDL_Surface* result=SDL_CreateRGBSurface(0,source->w/factor,source->h/factor,source->format->BitsPerPixel,source->format->Rmask,source->format->Gmask,source->format->Bmask,source->format->Amask);
92
    SDL_LockSurface(result);
93 94 95 96 97 98 99 100 101 102 103 104 105
    Uint32 alpha,red,green,blue;
    Uint32 color, colorPart;
    for(int i=0;i<result->w;i++)
        for(int j=0;j<result->h;j++)
        {
            color=readPixel(source,i*factor,j*factor);
            writePixel(result,i,j,color);
            /*for(int k=0;k<factor;k++)
                for(int l=0;l<factor;l++)
                {
                    readPixel(source,i+k,j+l);
                }*/
        }
106
    SDL_UnlockSurface(result);
107
    return result;
daFischer's avatar
daFischer committed
108
}