예제 #1
0
void LevelPlaySelect::renderTooltip(SDL_Renderer &renderer, unsigned int number, int dy){
    if (!toolTip.name || toolTip.number != number) {
        SDL_Color themeTextColor={0,0,0};
        const int SLEN = 64;
        char s[SLEN];

        //Render the name of the level.
        toolTip.name=textureFromText(renderer,*fontText,_CC(levels->getDictionaryManager(),levels->getLevelName(number)),themeTextColor);
        toolTip.time=nullptr;
        toolTip.recordings=nullptr;
        toolTip.number=number;

        //The time it took.
        if(levels->getLevel(number)->time>0){
            snprintf(s,SLEN,"%-.2fs",levels->getLevel(number)->time/40.0f);
            toolTip.time=textureFromText(renderer,*fontText,s,themeTextColor);//TTF_RenderUTF8_Blended(fontText,s,themeTextColor);
        }

        //The number of recordings it took.
        if(levels->getLevel(number)->recordings>=0){
            snprintf(s,SLEN,"%d",levels->getLevel(number)->recordings);
            toolTip.recordings = textureFromText(renderer,*fontText,s,themeTextColor);
        }
    }
	
    const SDL_Rect nameSize = rectFromTexture(*toolTip.name);
	//Now draw a square the size of the three texts combined.
	SDL_Rect r=numbers[number].box;
	r.y-=dy*64;
    if(toolTip.time && toolTip.recordings){
        const int recW = textureWidth(*toolTip.recordings);
        const int timeW = textureWidth(*toolTip.time);
        r.w=(nameSize.w)>(25+timeW+40+recW)?(nameSize.w):(25+timeW+40+recW);
        r.h=nameSize.h+5+20;
	}else{
        r.w=nameSize.w;
        r.h=nameSize.h;
	}
	
	//Make sure the tooltip doesn't go outside the window.
	if(r.y>SCREEN_HEIGHT-200){
        r.y-=nameSize.h+4;
	}else{
		r.y+=numbers[number].box.h+2;
	}
	if(r.x+r.w>SCREEN_WIDTH-50)
		r.x=SCREEN_WIDTH-50-r.w;
	
	//Draw a rectange
	Uint32 color=0xFFFFFFFF;
    drawGUIBox(r.x-5,r.y-5,r.w+10,r.h+10,renderer,color);
	
	//Calc the position to draw.
	SDL_Rect r2=r;
	
	//Now we render the name if the surface isn't null.
    if(toolTip.name){
		//Draw the name.
        applyTexture(r2.x, r2.y, toolTip.name, renderer);
	}
	//Increase the height to leave a gap between name and stats.
	r2.y+=30;
    if(toolTip.time){
		//Now draw the time.
        applyTexture(r2.x,r2.y,levelInfoRender.timeIcon,renderer);
		r2.x+=25;
        applyTexture(r2.x, r2.y, toolTip.time, renderer);
        r2.x+=textureWidth(*toolTip.time)+15;
	}
    if(toolTip.recordings){
		//Now draw the recordings.
        applyTexture(r2.x,r2.y,levelInfoRender.recordingsIcon,renderer);
		r2.x+=25;
        applyTexture(r2.x, r2.y, toolTip.recordings, renderer);
	}
}
예제 #2
0
파일: main.c 프로젝트: RylandAlmanza/piggle
int main(int argc, char* args[]) {
    // This will be the window we'll be rendering to
    SDL_Window *window = NULL;

    SDL_Renderer *renderer = NULL;

    TTF_Font *font;

    SDL_Texture *sheet = NULL;
    Text text;

    init_sprites();
    event_init();

    if (SDL_Init(SDL_INIT_VIDEO) < 0) {
        printf("SDL could not initialize! SDL_ERROR: %s\n", SDL_GetError());
    } else {
        //if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 1)) {
        //    printf("Warning: Linear texture filtering not enabled");
        //}
        window = SDL_CreateWindow("Piggle",
                                  SDL_WINDOWPOS_UNDEFINED,
                                  SDL_WINDOWPOS_UNDEFINED,
                                  SCREEN_WIDTH,
                                  SCREEN_HEIGHT,
                                  SDL_WINDOW_SHOWN);
        if (window == NULL) {
            printf("Window could not be created! SDL_Error: %s\n",
                   SDL_GetError());
        } else {
            renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
            if (renderer == NULL) {
                printf("Renderer couldn't be created. Shit's f****d, becuase %s"
                       ", dude.\n", SDL_GetError());
            } else {
                SDL_SetRenderDrawColor(renderer, 0xFF, 0xFF, 0xFF, 0xFF);

                int imgFlags = IMG_INIT_PNG;
                if (!(IMG_Init(imgFlags) & imgFlags)) {
                    printf("SDL_image couldn't be initialized. Shit's f****d, "
                           "because %s, dude.\n", IMG_GetError());
                }

                if (TTF_Init() == -1) {
                    printf("SDL_ttf couldn't initialize. Shit's f****d, becuase"
                           " %s, dude.\n", TTF_GetError());
                }
            }
            font = TTF_OpenFont("DroidSansMono.ttf", 16);
            if (font == NULL) {
                printf("Failed to load lazy font. Shit's f****d, because %s, "
                       "dude.", TTF_GetError());
            }
                
            sheet = loadTexture("img/sprites.png", renderer);
            // When quit is set to true, we'll stop running
            bool quit = false;
            piggle_scene_over = false;
            // Event handler
            SDL_Event sdl_event;

            // Set the current scene to the start scene
            piggle_scene_update = start_scene_update;

            while (!quit) {
                timer_start();
                EventList events = EventList_new();
                while (SDL_PollEvent(&sdl_event) != 0) {
                    Event event;
                    // If the user presses that little "x"
                    if (sdl_event.type == SDL_QUIT) {
                        quit = true;
                        event.type = QUIT;
                    } else if (sdl_event.type == SDL_KEYDOWN) {
                        event.type = KEYDOWN;
                        int key = sdl_event.key.keysym.sym;
                        event.value = event_value_from_key(key);
                    } else if (sdl_event.type == SDL_KEYUP) {
                        event.type = KEYUP;
                        int key = sdl_event.key.keysym.sym;
                        event.value = event_value_from_key(key);
                    } else {
                        continue;
                    }
                    events.add_event(&events, event);
                }
                SDL_RenderClear(renderer);
                DrawActionList actions = DrawActionList_new();
                piggle_scene_update(&events, &actions);
                events.destroy(&events);
                int i;
                for (i = 0; i < actions.length; i++) {
                    DrawAction action = actions.actions[i];
                    if (action.type == SPRITE) {
                        SDL_Rect *sprite = &sprites[action.sprite];
                        SDL_Rect dest;
                        dest.x = action.x;
                        dest.y = action.y;
                        dest.w = sprite->w;
                        dest.h = sprite->h;
                        SDL_RenderCopy(renderer, sheet, sprite, &dest);
                    } else if (action.type == TEXT) {
                        SDL_Color color = {action.text.red,
                                           action.text.green,
                                           action.text.blue};
                        textureFromText(action.text.text, color, &text, font,
                                        renderer);
                        SDL_Rect textRect;
                        textRect.x = action.x;
                        textRect.y = action.y;
                        textRect.w = text.width;
                        textRect.h = text.height;
                        SDL_RenderCopy(renderer, text.texture, NULL, &textRect);
                        SDL_DestroyTexture(text.texture);
                    } else if (action.type == RECTANGLE) {
                        SDL_Rect rect = {.x = action.x,
                                         .y = action.y,
                                         .w = action.rect.width,
                                         .h = action.rect.height};
                        SDL_SetRenderDrawColor(renderer, action.rect.red,
                                               action.rect.green,
                                               action.rect.blue, 255);
                        SDL_RenderFillRect(renderer, &rect);
                    }
                }
                SDL_RenderPresent(renderer);
                actions.destroy(&actions);
                if (piggle_scene_over) {
                    piggle_scene_update = piggle_scene_next;
                    piggle_scene_over = false;
                }
                if (timer_get_ticks() < 1000 / 60) {
                    SDL_Delay((1000 / 60) - timer_get_ticks());
                }
            }
        }
    }

    SDL_DestroyTexture(sheet);
    SDL_DestroyTexture(text.texture);
    text.texture = NULL;
    sheet = NULL;
    TTF_CloseFont(font);
    font = NULL;
    SDL_DestroyRenderer(renderer);
    SDL_DestroyWindow(window);
    window = NULL;
    renderer = NULL;

    // Quit SDL subsystems
    TTF_Quit();
    IMG_Quit();
    SDL_Quit();

    event_uninit();

    return 0;
}