/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */ /* * main.cc * Copyright (C) Frederik Mutzel 2009 <crazywater@> * * main.cc 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 3 of the License, or * (at your option) any later version. * * main.cc 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, see <http://www.gnu.org/licenses/>. */ #include <iostream> #include "SDL.h" #include "SDL_image.h" #include "SDL_ttf.h" #define WIDTH 640 #define HEIGHT 480 #define INFOBARHEIGHT 60 #define IMAGE "minipotcat.png" #define FONT "FreeSans.ttf" #define FONTSIZE 32 #define TITLE "potcat." #define BG_RED 255 #define BG_GREEN 255 #define BG_BLUE 255 SDL_Surface *screen, *image; SDL_Event event; SDL_Rect src, dest; TTF_Font* font; int imwidth, imheight, xpos, ypos, xvel, yvel, bgcolor = 0; long long unsigned int points; bool left_pressed, right_pressed, up_pressed, down_pressed; void StartMovement(SDL_KeyboardEvent *key) { switch(key->keysym.sym){ case SDLK_LEFT: left_pressed = true; break; case SDLK_RIGHT: right_pressed = true; break; case SDLK_UP: up_pressed = true; break; case SDLK_DOWN: down_pressed = true; break; } } void StopMovement(SDL_KeyboardEvent *key) { switch(key->keysym.sym){ case SDLK_LEFT: left_pressed = false; xvel = 0; break; case SDLK_RIGHT: right_pressed = false; xvel = 0; break; case SDLK_UP: up_pressed = false; break; case SDLK_DOWN: down_pressed = false; break; } } void Move() { bool onthefloor = ypos + imheight >= HEIGHT; //Gravity! if (!onthefloor) { yvel += 2; } else { yvel = 0; } if (up_pressed) { if(onthefloor) yvel -= 25; else yvel -= 1; } if (right_pressed) xvel += 1; if (left_pressed) xvel -= 1; if (down_pressed) yvel += 1; xpos += xvel; if (xpos < 0) xpos = 0; if (xpos > (WIDTH - imwidth)) xpos = WIDTH - imwidth; ypos += yvel; if (ypos < INFOBARHEIGHT) ypos = INFOBARHEIGHT; if (ypos > (HEIGHT - imheight)) ypos = HEIGHT - imheight; } void RedrawPanel() { char pointstring[1024]; SDL_Surface *resulting_text; sprintf(pointstring, "%llu", points); SDL_Color fontcolor = {0,0,0,255}; resulting_text = TTF_RenderText_Blended(font, pointstring, fontcolor); dest.x = WIDTH - resulting_text->w - 10; dest.y = 10; dest.w = 400; dest.h = 50; SDL_BlitSurface(resulting_text, NULL, screen, &dest); SDL_FreeSurface(resulting_text); } void Redraw() { SDL_FillRect(screen, NULL, bgcolor); dest.x = xpos; dest.y = ypos; dest.w = imwidth; dest.h = imheight; SDL_BlitSurface(image, &src, screen, &dest); RedrawPanel(); SDL_Flip(screen); } bool Initialize() { printf("Initializing SDL.\n"); if (SDL_Init(SDL_INIT_VIDEO) != 0) { fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError()); return false; } printf("Initializing window.\n"); screen = SDL_SetVideoMode(WIDTH, HEIGHT, 0, SDL_ANYFORMAT); if (screen == NULL) { fprintf(stderr, "Unable to set video mode: %s\n", SDL_GetError()); return false; } SDL_WM_SetCaption(TITLE, TITLE); printf("Loading image.\n"); SDL_Surface *temp; temp = IMG_Load(IMAGE); if (temp == NULL) { printf("Unable to load image: %s\n", SDL_GetError()); return false; } printf("Initializing fonts.\n"); if (TTF_Init() != 0) { printf("Unable to initialize SDL_ttf: %s \n", TTF_GetError()); return false; } font = TTF_OpenFont(FONT, FONTSIZE); if (font == NULL){ printf("Unable to load font: %s %s \n", FONT, TTF_GetError()); return false; } bgcolor = SDL_MapRGB(screen->format, BG_RED, BG_GREEN, BG_BLUE); image = SDL_DisplayFormatAlpha(temp); imwidth = image->w; imheight = image->h; src.x = 0; src.y = 0; src.w = imwidth; src.h = imheight; xpos = 0; ypos = HEIGHT - imheight; Redraw(); return true; } void UpdatePoints() { points++; } int main() { bool running = true; if(Initialize()) { while(running) { while(SDL_PollEvent(&event)) { switch(event.type) { case SDL_KEYDOWN: StartMovement(&event.key); break; case SDL_KEYUP: StopMovement(&event.key); break; case SDL_QUIT: running = false; break; } } Move(); UpdatePoints(); Redraw(); SDL_Delay(30); } } }