Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Copyright (C) 2011-2012 Ahmad Amarullah ( http://amarullz.com/ )
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- /*
- * AROMA CORE - Graphic Framebuffer Source
- *
- */
- /* Instance Pointer */
- static AFBP _afb=NULL;
- /* Set Graphic Instance */
- void afbSetInstance(AFBP new_instance){
- _afb = new_instance;
- }
- /* Get Current Instance */
- AFBP afb(){
- return _afb;
- }
- /* DP To Pixel */
- int aDP(int dp){
- return (dp * _afb->dp);
- }
- /* Init Graphic Framebuffer Instance */
- AFBP afbInit(int width, int height){
- /* Allocating Graphic Instance */
- AFBP me = (AFBP) malloc(sizeof(AFB));
- /* Init SDL */
- me->fb = SDL_SetVideoMode(width, height, 32, SDL_HWSURFACE);
- if (!me->fb) goto error; /* Exit If Error */
- /* Init Variables */
- me->w = width; /* Width */
- me->h = height; /* Height */
- me->sz = me->w * me->h; /* Width x Height */
- me->line = me->fb->pitch; /* Line Memory Size */
- me->canvas = aCanvas(me->w, me->h); /* Create Display Canvas */
- me->dp = floor(aMin(me->w, me->h) / 160); /* Calculate DP */
- me->depth = 32; /* Color Depth */
- me->pixsz = me->depth >> 3; /* Pixel size per byte */
- me->fb_sz = (me->sz * me->pixsz); /* FrameBuffer Size */
- me->buffer = (voidp) me->fb->pixels; /* Map Framebuffer */
- me->rgb_pos_normal = 0; /* RGB Position non normal as default */
- me->is32 = 1; /* It is 32bit Depth */
- me->stride = me->line - (me->w * me->pixsz); /* Calculate Stride Size */
- /* OK */
- goto ok;
- error:
- free(me);
- return NULL;
- ok:
- afbSetInstance(me); /* Set Default Instance */
- return me;
- }
- /* Release Framebuffer Instance */
- void afbRelease(AFBP me){
- if (me==NULL) me=_afb;
- if (me==NULL) return;
- /* Free display canvas */
- aCanvasFree(me->canvas);
- }
- /* Refresh Display Framebuffer */
- void afbRefresh(){
- /* Refresh Display */
- SDL_UpdateRect(_afb->fb, 0, 0, 0, 0);
- }
- /* Refresh Display Framebuffer per Area */
- void afbRefreshEx(int x, int y, int w, int h){
- /* Refresh Display */
- SDL_UpdateRect(_afb->fb, x, y, w, h);
- }
- /* Save display canvas into framebuffer */
- void afbSync(byte refresh){
- /* Lock Surface */
- SDL_LockSurface(_afb->fb);
- if (_afb->stride==0){
- /* is 32bit framebuffer - without alignment */
- memcpy(_afb->buffer,_afb->canvas->data,_afb->fb_sz);
- }
- else {
- /* is 32bit framebuffer - with alignment */
- aAlignBlt32((dwordp) _afb->buffer,
- (dwordp) _afb->canvas->data, _afb->w, _afb->h, _afb->stride, 0);
- }
- /* Unlock It */
- SDL_UnlockSurface(_afb->fb);
- /* Sync Now */
- if (refresh){
- afbRefresh();
- }
- }
- /* Save display canvas into framebuffer per Area */
- void afbSyncEx(int x, int y, int w, int h, byte refresh){
- /* Lock Surface */
- if (x<0){
- w-=x;
- x=0;
- }
- if (y<0){
- h-=y;
- y=0;
- }
- if (x+w>_afb->w) w = _afb->w-x;
- if (y+h>_afb->h) h = _afb->h-x;
- if ((w<0)||(h<1)) return;
- SDL_LockSurface(_afb->fb);
- int xbyte = x*_afb->pixsz;
- int dstrd = (_afb->w - (w+x)) * _afb->pixsz;
- /* Align Copy */
- aAlignBlt32((dwordp) (_afb->buffer + (_afb->line*y) + xbyte),
- (dwordp) _afb->canvas->data,
- w,
- h,
- _afb->stride + xbyte + dstrd,
- xbyte+dstrd
- );
- /* Unlock It */
- SDL_UnlockSurface(_afb->fb);
- /* Sync Now */
- if (refresh){
- afbRefreshEx(x, y, w, h);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment