sdldemo.cpp

00001 #include <SDL/SDL.h>
00002 #include <GLICT/container.h>
00003 #include <GLICT/window.h>
00004 #include <GLICT/button.h>
00005 #include <GLICT/fonts.h>
00006 #include <GLICT/globals.h>
00007 //#include "sdlfont.h"
00008 glictContainer desktop;
00009 glictWindow win;
00010 glictButton btn;
00011 SDL_Surface* screen;
00012 SDL_Surface* sysfontpic;
00013 void onclick(glictPos *a, glictContainer* callclass) {
00014         printf("...\n");
00015 }
00016 
00017 void SDLFontDrawChar(char t, SDL_Surface* img, int x1, int y1) {
00018         t -= 32;
00019     int x = (int)(t % 32)*16.;
00020     int y = (int)(t / 32)*16.;
00021 
00022     int w = 10;
00023     int h = 10;
00024 
00025         SDL_Rect dest;
00026         dest.x = x1;
00027         dest.y = y1;
00028         SDL_Rect src;
00029         src.x = x;
00030         src.y = y;
00031         src.w = w;
00032         src.h = h;
00033         SDL_BlitSurface(img, &src, screen, &dest);
00034 }
00035 void SDLFontDraw(const char* txt, const void* font, float x, float y){
00036 
00037         SDL_Surface *img = (SDL_Surface*)font;
00038 
00039         float cx=x*10,cy=y*10;
00040 
00041     volatile register float sizesofar = 0.;
00042         volatile register float linessofar = 0.;
00043         for (volatile register unsigned char *t = (unsigned char*)txt; *t; ++t) {
00044                 switch (*t) {
00045                         default:
00046 
00047                                 SDLFontDrawChar(*t, img, cx, cy);
00048                                 cx += 10;
00049                                 sizesofar += 10;
00050                                 break;
00051                         case '\n':
00052                         case '\r':
00053                                 cx -= sizesofar;
00054                                 cy += 10;
00055                                 linessofar += 1.;
00056                                 sizesofar = 0;
00057                                 if (*t == '\n' && *(t+1)=='\r' || *t == '\r' && *(t+1)=='\n' ) t++;
00058                 break;
00059 
00060                 }
00061         }
00062 
00063 
00064 }
00065 float SDLFontSize(const char* txt, const void* font) {
00066         return strlen(txt);
00067 }
00068 
00069 void SDLRectDraw(float left, float right, float top, float bottom, glictColor &col) {
00070         const SDL_VideoInfo* vi = SDL_GetVideoInfo();
00071         int color = SDL_MapRGB(vi->vfmt, (int)(col.r * 255), (int)(col.g * 255), (int)(col.b * 255));
00072         SDL_Rect rect = {left, top, right-left, bottom-top};
00073         SDL_FillRect(screen, &rect, color);
00074 }
00075 
00076 int main () {
00077         int videoflags = SDL_HWSURFACE  | SDL_ANYFORMAT| SDL_DOUBLEBUF | SDL_RESIZABLE;
00078         int width = 640;
00079         int height = 480;
00080         int video_bpp = 32;
00081 
00082 
00083         if(SDL_Init(SDL_INIT_VIDEO) < 0) {
00084                 fprintf(stderr,"Couldn't initialize SDL: %s\n", SDL_GetError());
00085                 exit(1);
00086         }
00087 
00088 
00089 
00090         screen = SDL_SetVideoMode(width, height, video_bpp, videoflags);
00091 
00092         if (!screen){
00093                 fprintf(stderr, "Could not set %dx%d video mode: %s\n", width, height, SDL_GetError());
00094                 exit(1);
00095         }
00096 
00097     printf("Set 640x480 at %d bits-per-pixel mode\n",
00098            screen->format->BitsPerPixel);
00099 
00100     glictFont* sysfont = glictCreateFont("system");
00101         sysfontpic = SDL_LoadBMP("font.bmp");
00102 
00103         SDL_SetColorKey(sysfontpic, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(SDL_GetVideoInfo()->vfmt, 0xFF, 0, 0xFF)); // magneta is transparent
00104 
00105     sysfont->SetFontParam(sysfontpic);
00106     sysfont->SetRenderFunc(SDLFontDraw);
00107     sysfont->SetSizeFunc(SDLFontSize);
00108 
00109 
00110         desktop.AddObject(&win);
00111         win.AddObject(&btn);
00112         win.SetPos(50, 50);
00113         btn.SetOnClick(onclick);
00114         btn.SetBGColor(.5,.5,.5,1.);
00115 
00116         glictGlobals.w = width;
00117         glictGlobals.h = height;
00118         desktop.SetWidth(width);
00119         desktop.SetHeight(height);
00120 
00121         glictGlobals.paintrectCallback = SDLRectDraw;
00122         glictGlobals.enableGlTranslate = false;
00123 
00124         SDL_WM_SetCaption("GLICT/SDL Demo", "");
00125         bool running = true;
00126         SDL_Event event;
00127 
00128         while(running){
00129                 while(SDL_PollEvent(&event)){
00130                         switch (event.type){
00131                                 case SDL_KEYDOWN:
00132 
00133                                         break;
00134                                 case SDL_MOUSEBUTTONUP:
00135                                 case SDL_MOUSEBUTTONDOWN:{
00136                                         glictPos p = {event.button.x, event.button.y};
00137                                         printf("%d %d\n", event.button.x, event.button.y);
00138                                         desktop.CastEvent(event.type == SDL_MOUSEBUTTONUP ? GLICT_MOUSEUP : GLICT_MOUSEDOWN, &p,0);
00139 
00140                                         break;
00141                                 }
00142                                 case SDL_QUIT:
00143                                                 running = false;
00144                                         break;
00145                                 default:
00146                                         break;
00147                         }
00148                 }
00149 
00150                 // blanks the screen..
00151                 glictColor c = {0,0,0,1};
00152                 SDLRectDraw(0,640,0,480,c);
00153 
00154                 desktop.Paint();
00155                 SDL_Flip(screen);
00156 
00157         }
00158 
00159         return 0;
00160 }

SourceForge.net Logo
generated with doxygen 1.5.3 on Mon Oct 29 18:09:26 2007