手機 - Blackberry Passport - Core Native (C/C++) - SDL v1.2 - Mouse Event



main.cpp

#include <stdio.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>

int main(int argc, char* args[])
{
    int loop = 1;
    SDL_Event event;

    if (SDL_Init(SDL_INIT_VIDEO) != 0) {
        printf("%s, failed to SDL_Init\n", __func__);
        return -1;
    }

    SDL_Surface* screen = NULL;
    screen = SDL_SetVideoMode(1440, 1440, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
    if (screen == NULL) {
        printf("%s, failed to SDL_SetVideMode\n", __func__);
        return -1;
    }
    SDL_ShowCursor(0);
    SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));

    if (TTF_Init() == -1) {
        printf("failed to TTF_Init\n");
        return -1;
    }

    SDL_Rect of = {0}; 
    SDL_Color textColor = {0xff, 0, 0};
    TTF_Font *font = TTF_OpenFont("app/native/lato.ttf", 100);
    SDL_Surface *msg = TTF_RenderText_Solid(font, "Lesson 9. Mouse Events", textColor);
  
    of.x = 100;
    of.y = 600;
    SDL_BlitSurface(msg, NULL, screen, &of);
    SDL_Flip(screen);
    while (loop) {
        while (SDL_PollEvent(&event)) {
            int flip = 1;
            char buf[128];

            if (event.type == SDL_KEYDOWN) {
                if (event.key.keysym.sym == SDLK_x) {
                    loop = 0;
                }
                sprintf(buf, "sym 0x%x pressed !", event.key.keysym.sym); 
            }
            else if (event.type == SDL_MOUSEMOTION) {
                sprintf(buf, "motion: %d, %d", event.motion.x, event.motion.y); 
            }
            else if (event.type == SDL_MOUSEBUTTONDOWN) {
                sprintf(buf, "button down: %d, %d", event.button.x, event.button.y); 
            }
            else if (event.type == SDL_MOUSEBUTTONUP) {
                sprintf(buf, "button up: %d, %d", event.button.x, event.button.y); 
            }
            else {
                flip = 0;
            }

            if (flip > 0) {
                SDL_FreeSurface(msg);
                SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0xff, 0xff, 0xff));
                msg = TTF_RenderText_Solid(font, buf, textColor);
                SDL_BlitSurface(msg, NULL, screen, &of);
                SDL_Flip(screen);
            }
        }
    }
    TTF_CloseFont(font);
    SDL_FreeSurface(msg);
    TTF_Quit();
    SDL_Quit();
    return 0;    
}

bar-descriptor.xml

<?xml version="1.0" encoding="utf-8" standalone="no"?>
<qnx xmlns="http://www.qnx.com/schemas/application/1.0">
    <id>com.steward.sdl.ch9</id>
    <name>ch9</name>
    <filename>ch9</filename>
    <versionNumber>1.0.0</versionNumber>
    <buildId>1</buildId>
    <description>Lesson 9. Mouse Events</description>

    <author>Steward</author>
    <authorId>gYAAgGE4qaHzBnzEAu8JKe4G1OI</authorId>

    <asset path="main" entry="true" type="Qnx/Elf">main</asset>
    <asset path="lato.ttf">lato.ttf</asset>
    <asset path="lib" type="Qnx/Elf">lib</asset>

    <permission system="true">run_native</permission>
    <env var="LD_LIBRARY_PATH" value="app/native/lib"/>
</qnx>

Makefile

main: main.cpp
	ntoarmv7-gcc main.cpp -g -o main lib/libSDL-1.2.so.11 -Iinclude lib/libSDL_image-1.2.so.8 lib/libSDL_ttf-2.0.so.10 $(QNX_TARGET)/lib/libwebp.a
	blackberry-nativepackager -package main.bar bar-descriptor.xml -devMode -debugToken ${HOME}/.rim/debugtoken_q30.bar

clean:
	rm -rf main
	rm -rf main.bar

完成