{"id":472,"date":"2010-09-28T22:13:00","date_gmt":"2010-09-28T22:13:00","guid":{"rendered":"http:\/\/metaviewsoft.de\/wordpress\/?p=472"},"modified":"2010-09-29T06:27:25","modified_gmt":"2010-09-29T06:27:25","slug":"a-little-example-app-to-my-previous-post-webos-howto-make-your-2d-sdl-app-palm-pixi-compatible","status":"publish","type":"post","link":"https:\/\/metaviewsoft.de\/wordpress\/?p=472","title":{"rendered":"A little example app to my previous post: #WebOS-HowTo: Make your 2D SDL app @Palm Pixi compatible."},"content":{"rendered":"<p>As promised I created a small example app which will show you a running example of how to make your 2D SDL app compatible with Pixi. Here it come, the source and also a complete (Windows VC++-based) project:<\/p>\n<p><!--more--><\/p>\n<pre><code>\r\n#include <stdio .h>\r\n#include <math .h>\r\n#include <time .h>\r\n\r\n#include <gles \/gl.h>\r\n#include <\/gles><gles \/glext.h>\r\n\r\n#include \"SDL.h\"\r\n#include \"PDL.h\"\r\n#include \"SDL_ttf.h\"\r\n#include \"SDL_image.h\"\r\n#include \"SDL_mixer.h\"\r\n\r\n\r\n#define GL_BGR  0x80E0\r\n#define GL_BGRA 0x80E1\r\n\r\n#ifdef _WINDOWS\r\n\t#define FONT \"C:\\\\WINDOWS\\\\Fonts\\\\Arial.ttf\"\r\n#else\r\n\t#define FONT \"\/usr\/share\/fonts\/PreludeCondensed-Medium.ttf\"\r\n#endif\r\n\r\n\r\n\/\/Surfaces\r\nSDL_Surface* Surface;\r\nSDL_Surface* Render;\r\nSDL_Surface* screen;\r\nTTF_Font *font_normal = NULL;\r\nMix_Chunk *explosion_sound = NULL;\r\nGLuint texture;\r\n\r\nint x, y;\r\ndouble v_x;\r\ndouble v_y;\r\ndouble a_x;\r\ndouble a_y;\r\n\r\n\/\/Misc\r\nPDL_ScreenMetrics screenMetrics;\r\nSDL_Event sdlEvent;\r\n\r\nvoid MyFillRect(int x, int y, int w, int h, int r, int g, int b, SDL_Surface* screen)\r\n{\r\n\tSDL_Rect rect = {x, y, w, h};\r\n\tSDL_FillRect(screen, &#038;rect, 0xff000000 | (r < < 16) | (g << 8) | b);\r\n}\r\n\r\nSDL_Surface *CreateSurface(Uint32 flags, int w, int h)\r\n{\r\n\treturn SDL_CreateRGBSurface(flags, w, h, Surface->format->BitsPerPixel, Surface->format->Rmask, Surface->format->Gmask, Surface->format->Bmask, Surface->format->Amask);\r\n}\r\n\r\nvoid UpdateDisplay(SDL_Surface *surface)\r\n{\r\n#ifdef _WIN32\r\n\tSDL_Flip(surface);\r\n#else\r\n\tint mode;\r\n\r\n\tglClear (GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);\r\n\r\n\tif (surface->format->BytesPerPixel == 3) { \/\/ RGB 24bit\r\n\t\tmode = GL_BGR;\r\n\t} else if (surface->format->BytesPerPixel == 4) { \/\/ RGBA 32bit\r\n\t\tmode = GL_BGRA;\r\n\t}\r\n\tglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);\r\n\tglTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);\r\n\tglTexImage2D(GL_TEXTURE_2D, 0, mode, surface->w, surface->h, 0, mode, GL_UNSIGNED_BYTE, surface->pixels);\r\n\r\n\tGLfloat box[] = {0,surface->h,0, surface->w,surface->h,0, surface->w,0,0, 0,0,0};\r\n\tGLfloat tex[] = {0,1, 1,1, 1,0, 0,0};\r\n\tglVertexPointer(3, GL_FLOAT, 0, box);\r\n\tglTexCoordPointer(2, GL_FLOAT, 0, tex);\r\n\tglDrawArrays(GL_TRIANGLE_FAN, 0, 4);\r\n\r\n\tglFlush ();\r\n\tSDL_GL_SwapBuffers();\r\n#endif\r\n}\r\n\r\nvoid ApplySurface( int x, int y, SDL_Surface* source, SDL_Surface* destination )\r\n{\r\n\tif (!source)\r\n\t\treturn;\r\n\r\n\t\/\/Holds offsets\r\n\tSDL_Rect offset;\r\n\t\r\n\t\/\/Source rect\r\n\tSDL_Rect src;\r\n\t\r\n\t\/\/Get offsets\r\n\toffset.x = x;\r\n\toffset.y = y;\r\n\t\r\n\tsrc.x = 0;\r\n\tsrc.y = 0;\r\n\tsrc.w = source->w;\r\n\tsrc.h = source->h;\r\n\t\r\n\t\/\/Blit\r\n\tSDL_BlitSurface( source, &#038;src, destination, &#038;offset );\r\n}\r\n\r\nvoid DisplayString(char *text, int left, int top, int width, int height, SDL_Color clr1, SDL_Color clr2)\r\n{\r\n\tMyFillRect(left, top, width, height, clr1.r, clr1.g, clr1.b, screen);\r\n\r\n    SDL_Color color = clr2;\r\n\tSDL_Surface *nr0 = TTF_RenderText_Blended( font_normal, text, color );\r\n\tif (nr0)\r\n\t{\r\n\t\tApplySurface( left + (width-nr0->w)\/2, top + (height-nr0->h)\/2, nr0, screen );\r\n\t\tSDL_FreeSurface(nr0);\r\n\t}\r\n}\r\n\r\nvoid DrawScreen()\r\n{\r\n\tbool result = true;\r\n\r\n\t\/\/Clear the entire screen\r\n\tMyFillRect(0, 0, screenMetrics.horizontalPixels, screenMetrics.verticalPixels, 0, 0, 0, screen);\r\n\r\n\t\/\/Draw a square\r\n\tMyFillRect(x, y, 20, 20, 255, 255, 255, screen);\r\n}\r\n\r\nint main(int argc, char** argv)\r\n{\r\n\tSDL_Joystick *gJoystick;\r\n\tbool bInitialUpdate = true;\r\n\tdouble initial_x = 0.0;\r\n\tdouble initial_y = 0.0;\r\n\r\n\t\/\/Init\r\n    SDL_Init(SDL_INIT_VIDEO | SDL_INIT_AUDIO | SDL_INIT_JOYSTICK);\r\n    PDL_Init(0);\r\n\r\n\tMix_Init(0);\r\n\tMix_OpenAudio(44100, AUDIO_S16SYS, 2, 1024);\r\n\tMix_AllocateChannels(20);\r\n\tMix_Volume(-1, 64);\r\n\r\n\texplosion_sound = Mix_LoadWAV(\"firework.wav\");\r\n\r\n\tTTF_Init();\r\n\tfont_normal = TTF_OpenFont( FONT, 11 );\r\n\tTTF_SetFontStyle(font_normal, TTF_STYLE_BOLD);\r\n\r\n\tgJoystick = SDL_JoystickOpen(0);\r\n\r\n\t\/\/Set screen resolution and check for errors\r\n\tPDL_Err err = PDL_GetScreenMetrics(&#038;screenMetrics);\r\n\t\/\/screenMetrics.verticalPixels = 400;\r\n#if WIN32\r\n\tif(err == PDL_INVALIDINPUT)\r\n\t\treturn -1;\r\n\tif(err == PDL_EOTHER)\r\n\t    screen = SDL_SetVideoMode(320, 480, 32, SDL_SWSURFACE);\r\n\telse\r\n\t\tscreen = SDL_SetVideoMode(screenMetrics.horizontalPixels, screenMetrics.verticalPixels, 24, SDL_SWSURFACE);\r\n#else\r\n\t\/\/ Tell it to use OpenGL version 1.0\r\n\tSDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 1);\r\n\t\/\/SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1);\r\n\r\n\t\/\/ Set the video mode to full screen\r\n\tSurface = SDL_SetVideoMode(screenMetrics.horizontalPixels, screenMetrics.verticalPixels, 0, SDL_OPENGL);\r\n\r\n\tRender = CreateSurface(0, 512, 512);\r\n\tscreen = Render;\r\n\r\n\t\/\/ setup openGL\r\n\tglClearColor(0.0f, 0.0f, 0.0f, 0.0f);\r\n\tglViewport( 0, 0, screenMetrics.horizontalPixels, screenMetrics.verticalPixels );\r\n\tglMatrixMode(GL_PROJECTION);\r\n\tglLoadIdentity();\r\n\tglOrthof(0, screenMetrics.horizontalPixels, screenMetrics.verticalPixels, 0, -1, 1);\r\n\r\n\tglMatrixMode(GL_MODELVIEW);\r\n\tglLoadIdentity();\r\n\r\n\tglEnable( GL_TEXTURE_2D );\r\n\tglEnableClientState(GL_VERTEX_ARRAY);\r\n\tglEnableClientState(GL_TEXTURE_COORD_ARRAY);\r\n\tglDisable(GL_DEPTH_TEST);\r\n\r\n\tglGenTextures(1, &#038;texture);\r\n\tglBindTexture(GL_TEXTURE_2D, texture);\r\n\r\n#endif\r\n\r\n\t\/\/ Set initial positions\r\n\tx = (screenMetrics.horizontalPixels - 20) \/ 2;\r\n\ty = (screenMetrics.verticalPixels - 20) \/ 2;\r\n\r\n\t\/\/The Event processing loop\r\n\tdo {\r\n\t\t\/\/Loop through all of the current events\r\n\t\twhile (SDL_PollEvent(&#038;sdlEvent)) {\r\n\t\t\tif (sdlEvent.type == SDL_KEYDOWN) {\r\n\t\t\t\t\/\/Read the actual key that is down\r\n\t\t\t\tswitch(sdlEvent.key.keysym.sym) {\r\n\r\n\t\t\t\t\t\/\/Handle the escape key by exiting the program\r\n\t\t\t\t\tcase SDLK_ESCAPE:\r\n\t\t\t\t\t\tsdlEvent.type = SDL_QUIT;\r\n\t\t\t\t\t\tbreak;\r\n\r\n\t\t\t\t\tdefault: break;\r\n\t\t\t\t}\r\n\t\t\t} else if (sdlEvent.type == SDL_JOYAXISMOTION) {\r\n\t\t\t\tif (bInitialUpdate) {\r\n\t\t\t\t\tdouble val = sdlEvent.jaxis.value \/ 32768.0;\r\n\t\t\t\t\tif (sdlEvent.jaxis.axis == 0) {\r\n\t\t\t\t\t\tinitial_x = val;\r\n\t\t\t\t\t} else if (sdlEvent.jaxis.axis == 1) {\r\n\t\t\t\t\t\tinitial_y = val;\r\n\t\t\t\t\t}\r\n\t\t\t\t\tif ((initial_x != 0.0) &#038;& (initial_y != 0.0)) {\r\n\t\t\t\t\t\tbInitialUpdate = false;\r\n\t\t\t\t\t}\r\n\t\t\t\t} else {\r\n\t\t\t\t\tdouble val = sdlEvent.jaxis.value \/ 32768.0;\r\n\t\t\t\t\tif (sdlEvent.jaxis.axis == 0) {\r\n\t\t\t\t\t\tval -= initial_x;\r\n\t\t\t\t\t\ta_x += val * val * val;\r\n\t\t\t\t\t} else if (sdlEvent.jaxis.axis == 1) {\r\n\t\t\t\t\t\tval -= initial_y;\r\n\t\t\t\t\t\ta_y += val * val * val;\r\n\t\t\t\t\t}\r\n\t\t\t\t}\r\n\t\t\t}\r\n\t\t}\r\n\r\n\t\tv_x += a_x;\r\n\t\tv_y += a_y;\r\n\t\tx += v_x;\r\n\t\ty += v_y;\r\n\r\n#define ABSORPTION 0.5\r\n\t\tif (x < 0) {\r\n\t\t\tx = 0;\r\n\t\t\tv_x = -v_x * ABSORPTION;\r\n\t\t\ta_x = 0;\r\n\t\t\tMix_PlayChannel(-1, explosion_sound, 0);\r\n\t\t}\r\n\t\tif (x > (screenMetrics.horizontalPixels-20)) {\r\n\t\t\tx = screenMetrics.horizontalPixels-20;\r\n\t\t\tv_x = -v_x * ABSORPTION;\r\n\t\t\ta_x = 0;\r\n\t\t\tMix_PlayChannel(-1, explosion_sound, 0);\r\n\t\t}\r\n\t\tif (y < 0) {\r\n\t\t\ty = 0;\r\n\t\t\tv_y = -v_y * ABSORPTION;\r\n\t\t\ta_y = 0;\r\n\t\t\tMix_PlayChannel(-1, explosion_sound, 0);\r\n\t\t}\r\n\t\tif (y > (screenMetrics.verticalPixels-20)) {\r\n\t\t\ty = screenMetrics.verticalPixels-20;\r\n\t\t\tv_y = -v_y * ABSORPTION;\r\n\t\t\ta_y = 0;\r\n\t\t\tMix_PlayChannel(-1, explosion_sound, 0);\r\n\t\t}\r\n\r\n\t\tDrawScreen();\r\n\t\tUpdateDisplay(screen);\r\n\r\n\t\t\/\/Don't be a cpu hog\r\n\t\tSDL_Delay(1);\r\n\t} while (sdlEvent.type != SDL_QUIT);\r\n\r\n\tif (explosion_sound)\r\n\t\tMix_FreeChunk(explosion_sound);\r\n\r\n\tMix_CloseAudio ();\r\n\tMix_Quit();\r\n\r\n\tTTF_Quit();\r\n    PDL_Quit();\r\n    SDL_Quit();\r\n\r\n    return 0;\r\n}\r\n<\/gles><\/time><\/math><\/stdio><\/code><\/pre>\n<p>Feel free to use it to convert your own app and don&#8217;t forget to download: <a href=\"http:\/\/developer.palm.com\/appredirect\/?packageid=de.metaviewsoft.mandelbrot\">Mandel<\/a>, <a href=\"http:\/\/developer.palm.com\/appredirect\/?packageid=de.metaviewsoft.crimson\">Crimson Fields<\/a> and <a href=\"http:\/\/developer.palm.com\/appredirect\/?packageid=de.metaviewsoft.prankapp\">Prankster<\/a> from the AppCatalog for free.<\/p>\n<p>Here is the project for you to download: <p><img src=\"http:\/\/metaviewsoft.de\/wordpress\/wp-content\/plugins\/downloadmanager\/images\/drive_go.gif\" alt=\"Download: simple\" title=\"Download: simple\" style=\"vertical-align: middle;\" \/>&nbsp;&nbsp;<strong><a href=\"http:\/\/metaviewsoft.de\/wordpress\/?dl_id=35\" title=\"Download: simple\">simple<\/a><\/strong> (2,3 MiB, 1.231 hits)<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>As promised I created a small example app which will show you a running example of how to make your 2D SDL app compatible with Pixi. Here it come, the source and also a complete (Windows VC++-based) project:<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[4],"tags":[],"views":6431,"_links":{"self":[{"href":"https:\/\/metaviewsoft.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/472"}],"collection":[{"href":"https:\/\/metaviewsoft.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/metaviewsoft.de\/wordpress\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/metaviewsoft.de\/wordpress\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/metaviewsoft.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=472"}],"version-history":[{"count":6,"href":"https:\/\/metaviewsoft.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/472\/revisions"}],"predecessor-version":[{"id":478,"href":"https:\/\/metaviewsoft.de\/wordpress\/index.php?rest_route=\/wp\/v2\/posts\/472\/revisions\/478"}],"wp:attachment":[{"href":"https:\/\/metaviewsoft.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=472"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/metaviewsoft.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=472"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/metaviewsoft.de\/wordpress\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=472"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}