/** * hdaps_tux - Move tux around by using your IBM Thinkpad * * Copyright (c) 2006 Tobias Klauser * * This code is based on gtollina.c from [1] and the SDL example from [2] * * [1] http://www.gnome.org/~fherrera/gtollina.c * [2] http://de.wikibooks.org/wiki/SDL * * This program is free software; you can redistribute it and/or modify it under * the terms of the GNU General Public License as published by the Free Software * Foundation; either version 2, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more * details. * * You should have received a copy of the GNU General Public License along with * this program; if not, write to the Free Software Foundation, Inc., 51 * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ #include #include #include #include #include #include "hdaps-tux.h" #define BUF_SIZ 256 int get_pos(int *xpos, int *ypos) { int fd; size_t ret; char buf[BUF_SIZ]; memset(&buf, 0, BUF_SIZ); fd = open(POSITION_FILE, O_RDONLY); if (fd < 0) { fprintf(stderr, "Could not open hdaps position file.\n"); exit(1); } ret = read(fd, &buf, BUF_SIZ); close(fd); if (ret < 0) return -1; sscanf(buf, "(%d,%d)", xpos, ypos); dprintf("(%d,%d)\n", *xpos, *ypos); return 0; } int main(int argc, char *argv[]) { SDL_Surface *screen, *image; SDL_Rect dst, dstwhite; SDL_Event event; Uint8 *keys; int tuxX = 0, tuxY = 0; int hdapsX = 0, hdapsY = 0; int hdapsXprev = 0, hdapsYprev = 0; int vel = 4; unsigned int timeout = TIMEOUT_VAL; char done = 0; /* Initialize SDL */ if (SDL_Init(SDL_INIT_VIDEO) == -1) { fprintf(stderr, "Can't init SDL: %s\n", SDL_GetError()); exit(1); } atexit(SDL_Quit); SDL_WM_SetCaption ("hdaps-tux - Move tux with your Thinkpad (press q to exit)", "hdaps-tux"); screen = SDL_SetVideoMode(W_WIDTH, W_HEIGHT, 16, SDL_HWSURFACE | SDL_DOUBLEBUF); SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 255, 255, 255)); if (!screen) { printf("Can't set video mode: %s\n", SDL_GetError()); exit(1); } image = SDL_LoadBMP(TUX_FILE); if (!image) { printf("Can't load '%s': %s\n", TUX_FILE, SDL_GetError()); exit(1); } dst.w = image->w; dst.h = image->h; dstwhite.w = image->w + 1; dstwhite.h = image->h + 1; SDL_SetColorKey(image, SDL_SRCCOLORKEY, SDL_MapRGB(image->format, 255, 255, 255)); /* Calculate and set initial position of tux (centre of the window) */ tuxX = (W_WIDTH / 2) - (image->w / 2); tuxY = (W_HEIGHT / 2) - (image->h / 2); /* Get the hdaps position so we have something to start from */ get_pos(&hdapsX, &hdapsY); while (!done) { while (SDL_PollEvent(&event)) { switch(event.type) { case SDL_QUIT: done = 1; break; } } dstwhite.x = tuxX; dstwhite.y = tuxY; /* Hack! */ if (--timeout == 0) { int xdiff = 0, ydiff = 0; hdapsXprev = hdapsX; hdapsYprev = hdapsY; get_pos(&hdapsX, &hdapsY); xdiff = hdapsX - hdapsXprev; ydiff = hdapsY - hdapsYprev; dprintf("xdiff/ydiff: %d/%d\n", xdiff, ydiff); tuxX += xdiff * vel; tuxY += ydiff * vel; timeout = TIMEOUT_VAL; } keys = SDL_GetKeyState(NULL); if (keys[SDLK_UP]) /* Manually move tux up */ if (tuxY > 5) tuxY--; if (keys[SDLK_DOWN]) /* Manually move tux down */ if (tuxY < W_HEIGHT - 5 - image->h) tuxY++; if (keys[SDLK_RIGHT]) /* Manually move tux right */ if (tuxX < W_WIDTH - 5 - image->w) tuxX++; if (keys[SDLK_LEFT]) /* Manually move tux left */ if (tuxX > 5) tuxX--; if (keys[SDLK_q]) /* Quit */ done = 1; if (tuxX < 5) tuxX = 5; if (tuxX > W_WIDTH - image->w) tuxX = W_WIDTH - image->w; if (tuxY < 5) tuxY = 5; if (tuxY > W_HEIGHT - image->h) tuxY = W_HEIGHT - image->h; dprintf("tuxX/tuxY: %d/%d\n", tuxX, tuxY); dst.x = tuxX; dst.y = tuxY; SDL_FillRect(screen, &dstwhite, SDL_MapRGB(screen->format, 255, 255, 255)); SDL_BlitSurface(image, NULL, screen, &dst); SDL_Flip(screen); } SDL_FreeSurface(image); return 0; }