ya2d_Texture* ya2d_createTexture(int w, int h, int psm, ya2d_Place place) { ya2d_Texture* texp = (ya2d_Texture*)malloc(sizeof(ya2d_Texture)); texp->imageWidth = w; texp->imageHeight = h; texp->textureWidth = next_pow2(w); texp->textureHeight = next_pow2(h); texp->centerX = (int)(w/2); texp->centerY = (int)(h/2); texp->texPSM = psm; texp->isSwizzled = 0; texp->hasAlpha = 0; switch(psm) { case GU_PSM_4444: case GU_PSM_5650: case GU_PSM_5551: texp->rowBytes = texp->textureWidth * 2; break; case GU_PSM_8888: default: texp->rowBytes = texp->textureWidth * 4; break; } texp->dataLength = texp->rowBytes * texp->textureHeight; if(place == YA2D_VRAM) { if(texp->dataLength <= vlargestblock()) //enough free VRAM space { texp->data = valloc(texp->dataLength); texp->rel_ptr = vrelptr(texp->data); texp->place = YA2D_VRAM; return texp; } } texp->data = malloc(texp->dataLength); texp->rel_ptr = texp->data; texp->place = YA2D_RAM; return texp; }
void* valloc( size_t size ) { // Initialize memory block, if not yet done if (__mem_blocks[0]==0) __mem_blocks[0] = __BLOCK0; int i = 0; int j = 0; int bsize = __BLOCKS(size); if (__largest_update==0 && __largest_block<bsize) { printf("Not enough memory to allocate %i bytes (largest: %i)!\n",size,vlargestblock()); return(0); } printf("allocating %i bytes, in %i blocks\n", size, bsize); // Find smallest block that still fits the requested size int bestblock = -1; int bestblock_prev = 0; int bestblock_size = __MEM_BLOCKS+1; while (i<__MEM_BLOCKS) { int csize = __BLOCK_GET_SIZE(__mem_blocks[i]); if (__BLOCK_GET_FREEBLOCK(__mem_blocks[i])==3 && csize>=bsize) { if (csize<bestblock_size) { bestblock = i; bestblock_prev = j; bestblock_size = csize; } if (csize==bsize) break; } j = i; i += csize; } if (bestblock<0) { printf("Not enough memory to allocate %i bytes (largest: %i)!\n",size,vlargestblock()); return(0); } i = bestblock; j = bestblock_prev; int csize = bestblock_size; __mem_blocks[i] = __BLOCK_MAKE(bsize,j,0,1); int next = i+bsize; if (csize>bsize && next<__MEM_BLOCKS) { __mem_blocks[next] = __BLOCK_MAKE(csize-bsize,i,1,1); int nextnext = i+csize; if (nextnext<__MEM_BLOCKS) { __BLOCK_SET_PREV(__mem_blocks[nextnext], next); } } __mem_free -= bsize; if (__largest_block==csize) // if we just allocated from one of the largest blocks { if ((csize-bsize)>(__mem_free/2)) __largest_block = (csize-bsize); // there can't be another largest block else __largest_update = 1; } return ((void*)(__MEM_START + (i*__BLOCK_SIZE))); }
s32 main(s32 argc, const char* argv[]) { ya2d_init(); ya2d_Texture *texture1 = ya2d_loadPNGfromBuffer((void *)pngsample_png, pngsample_png_size); ya2d_Texture *texture2 = ya2d_loadPNGfromBuffer((void *)alphaimg_png, alphaimg_png_size); int x = 50, y = 100; int size = 50; float angle = 0.0f; while(1) { ya2d_screenClear(); ya2d_screenBeginDrawing(); ya2d_controlsRead(); DrawFormatString(15, 15, "YA2D LIB -- by xerpi, thanks to deaphroat, harryoke and Veritassdg for testing ;)"); DrawFormatString(15, 35, "FPS: %.2f available vram: %i largest block: %i", ya2d_screenFPS(), vmemavail(), vlargestblock()); DrawFormatString(15, 55, "Press START to exit."); if(ya2d_paddata[0].BTN_RIGHT) x+=10; if(ya2d_paddata[0].BTN_LEFT) x-=10; if(ya2d_paddata[0].BTN_DOWN) y+=10; if(ya2d_paddata[0].BTN_UP) y-=10; if(ya2d_paddata[0].BTN_CROSS) ya2d_drawFillRect(200, 350, 100, 100, rand()); if(x > (SCREEN_W-size)) x = SCREEN_W-size; if(x<0) x = 0; if(y > (SCREEN_H-size)) y = SCREEN_H-size; if(y<0) y = 0; ya2d_drawFillRect(x, y, size, size, 0xFF0000FF); ya2d_drawRotateTexture(texture2, 50, 90, angle += 0.1f); ya2d_drawTexture(texture1, 100, 50); ya2d_screenFlip(); if(ya2d_paddata[0].BTN_START) break; } ya2d_freeTexture(texture1); ya2d_freeTexture(texture2); ya2d_deinit(); return 0; }