amarullz

sdl aroma

Jul 14th, 2015
232
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.14 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2011-2012 Ahmad Amarullah ( http://amarullz.com/ )
  3.  *
  4.  * Licensed under the Apache License, Version 2.0 (the "License");
  5.  * you may not use this file except in compliance with the License.
  6.  * You may obtain a copy of the License at
  7.  *
  8.  *      http://www.apache.org/licenses/LICENSE-2.0
  9.  *
  10.  * Unless required by applicable law or agreed to in writing, software
  11.  * distributed under the License is distributed on an "AS IS" BASIS,
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13.  * See the License for the specific language governing permissions and
  14.  * limitations under the License.
  15.  */
  16.  
  17. /*
  18.  * AROMA CORE - Graphic Framebuffer Source
  19.  *
  20.  */
  21.  
  22. /* Instance Pointer */
  23. static AFBP _afb=NULL;
  24.    
  25. /* Set Graphic Instance */
  26. void afbSetInstance(AFBP new_instance){
  27.   _afb = new_instance;
  28. }
  29.  
  30. /* Get Current Instance */
  31. AFBP afb(){
  32.   return _afb;
  33. }
  34.  
  35. /* DP To Pixel */
  36. int aDP(int dp){
  37.   return (dp * _afb->dp);
  38. }
  39.  
  40. /* Init Graphic Framebuffer Instance */
  41. AFBP afbInit(int width, int height){
  42.   /* Allocating Graphic Instance */
  43.   AFBP me = (AFBP) malloc(sizeof(AFB));
  44.  
  45.   /* Init SDL */
  46.   me->fb    = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE);
  47.   if (!me->fb) goto error; /* Exit If Error */
  48.  
  49.   /* Init Variables */
  50.   me->w       = width;                              /* Width */
  51.   me->h       = height;                             /* Height */
  52.   me->sz      = me->w * me->h;                      /* Width x Height */
  53.   me->line    = me->fb->pitch;                      /* Line Memory Size */
  54.   me->canvas  = aCanvas(me->w, me->h);              /* Create Display Canvas */
  55.   me->dp      = floor(aMin(me->w, me->h) / 160);    /* Calculate DP */
  56.   me->depth   = 32;                                 /* Color Depth */
  57.   me->pixsz   = me->depth >> 3;                     /* Pixel size per byte */
  58.   me->fb_sz   = (me->sz * me->pixsz);               /* FrameBuffer Size */
  59.   me->buffer  = (voidp) me->fb->pixels;             /* Map Framebuffer */
  60.   me->rgb_pos_normal  = 0;                          /* RGB Position non normal as default */
  61.   me->is32    = 1;                                  /* It is 32bit Depth */
  62.   me->stride = me->line - (me->w * me->pixsz);      /* Calculate Stride Size */
  63.  
  64.   /* OK */
  65.   goto ok;
  66. error:
  67.   free(me);
  68.   return NULL;
  69. ok:
  70.   afbSetInstance(me); /* Set Default Instance */
  71.   return me;
  72. }
  73.  
  74. /* Release Framebuffer Instance */
  75. void afbRelease(AFBP me){
  76.    if (me==NULL) me=_afb;
  77.    if (me==NULL) return;
  78.    
  79.    /* Free display canvas */
  80.    aCanvasFree(me->canvas);
  81. }
  82.  
  83. /* Refresh Display Framebuffer */
  84. void afbRefresh(){
  85.   /* Refresh Display */
  86.   SDL_UpdateRect(_afb->fb, 0, 0, 0, 0);
  87. }
  88.  
  89. /* Refresh Display Framebuffer per Area */
  90. void afbRefreshEx(int x, int y, int w, int h){
  91.   /* Refresh Display */
  92.   SDL_UpdateRect(_afb->fb, x, y, w, h);
  93. }
  94.  
  95. /* Save display canvas into framebuffer */
  96. void afbSync(byte refresh){
  97.   /* Lock Surface */
  98.   SDL_LockSurface(_afb->fb);
  99.   if (_afb->stride==0){
  100.     /* is 32bit framebuffer - without alignment */
  101.     memcpy(_afb->buffer,_afb->canvas->data,_afb->fb_sz);
  102.   }
  103.   else {
  104.     /* is 32bit framebuffer - with alignment */
  105.     aAlignBlt32((dwordp) _afb->buffer,
  106.       (dwordp) _afb->canvas->data, _afb->w, _afb->h, _afb->stride, 0);
  107.   }
  108.   /* Unlock It */
  109.   SDL_UnlockSurface(_afb->fb);
  110.  
  111.   /* Sync Now */
  112.   if (refresh){
  113.     afbRefresh();
  114.   }
  115. }
  116.  
  117. /* Save display canvas into framebuffer per Area */
  118. void afbSyncEx(int x, int y, int w, int h, byte refresh){
  119.   /* Lock Surface */
  120.   if (x<0){
  121.     w-=x;
  122.     x=0;
  123.   }
  124.   if (y<0){
  125.     h-=y;
  126.     y=0;
  127.   }
  128.   if (x+w>_afb->w) w = _afb->w-x;
  129.   if (y+h>_afb->h) h = _afb->h-x;
  130.   if ((w<0)||(h<1)) return;
  131.   SDL_LockSurface(_afb->fb);
  132.  
  133.   int xbyte = x*_afb->pixsz;
  134.   int dstrd = (_afb->w - (w+x)) * _afb->pixsz;
  135.  
  136.   /* Align Copy */
  137.   aAlignBlt32((dwordp) (_afb->buffer + (_afb->line*y) + xbyte),
  138.       (dwordp) _afb->canvas->data,
  139.       w,
  140.       h,
  141.       _afb->stride + xbyte + dstrd,
  142.       xbyte+dstrd
  143.   );
  144.  
  145.   /* Unlock It */
  146.   SDL_UnlockSurface(_afb->fb);
  147.  
  148.   /* Sync Now */
  149.   if (refresh){
  150.     afbRefreshEx(x, y, w, h);
  151.   }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment