Exemple #1
0
static int s_AllocateGraphID(int textureRefID, SDL_Rect rect, int linkToGraphID) {
    int graphID;
    Graph *graph, *linkedGraph;
    
    graphID = PL_Handle_AcquireID(DXHANDLE_GRAPH);
    if (graphID < 0) {
        return -1;
    }
    
    PL_Texture_AddRef(textureRefID);
    
    graph = (Graph *)PL_Handle_AllocateData(graphID, sizeof(Graph));
    graph->textureRefID = textureRefID;
    graph->rect = rect;
    
    if (linkToGraphID < 0 || (linkedGraph = s_GetGraph(linkToGraphID)) == NULL) {
        graph->prevLinkedGraphID = -1;
        graph->nextLinkedGraphID = -1;
    } else {
        Graph *nextGraph = s_GetGraph(linkedGraph->nextLinkedGraphID);
        graph->prevLinkedGraphID = linkToGraphID;
        graph->nextLinkedGraphID = linkedGraph->nextLinkedGraphID;
        linkedGraph->nextLinkedGraphID = graphID;
        if (nextGraph != NULL) {
            nextGraph->prevLinkedGraphID = graphID;
        }
    }
    
    s_graphCount += 1;
    
    return graphID;
}
static int s_AllocateTextureRefID(SDL_Texture *texture) {
    int textureID = PL_Handle_AcquireID(DXHANDLE_TEXTURE);
    TextureRef *textureref;
    
    if (textureID < 0) {
        return -1;
    }
    
    textureref = (TextureRef *)PL_Handle_AllocateData(textureID, sizeof(TextureRef));
    textureref->texture = texture;
    textureref->refCount = 0;
    
    SDL_QueryTexture(texture, &textureref->format, NULL, NULL, NULL);
    
    return textureID;
}
Exemple #3
0
int Dx_FileRead_open(const char *filename) {
    SDL_RWops *rwops = Dx_File_OpenStream(filename);
    int fileDataID;
    FileHandle *handle;
    
    if (rwops == NULL) {
        return -1;
    }

    fileDataID = PL_Handle_AcquireID(DXHANDLE_FILE);
    if (fileDataID < 0) {
        SDL_RWclose(rwops);
        return -1;
    }
    
    handle = (FileHandle *)PL_Handle_AllocateData(fileDataID, sizeof(FileHandle));
    handle->rwops = rwops;
    
    return fileDataID;
}
LFONTSPRITE LunaFontSprite::CreateFromFile(const char *pFileName,
                                 const char *pExt, Bool IsAlpha, Uint32 Num,
                                 Bool IsSortZ,
                                 eSurfaceFormat Format) {
    char filebuf[4096];
    int file = PL_File_OpenRead(
        PL_Text_ConvertStrncpyIfNecessary(
            filebuf, -1, pFileName, g_lunaUseCharSet, 4096)
    );
    unsigned char *lfdData = NULL;
    
    do {
        int64_t fileSize = PL_File_GetSize(file);
        const LFDHeader *lfdHeader;
        LunaFontSprData *fontspr;
        int fontSprHandle;
        int fontBaseSize;
        int i;
        
        /* don't allow files beyond a certain size */
        if (file < 0 || fileSize <= 0 || fileSize > 65536) {
            break;
        }
        
        lfdData = (unsigned char *)DXALLOC((int)fileSize);
        if (PL_File_Read(file, lfdData, (int)fileSize) != (int)fileSize) {
            break;
        }
        PL_File_Close(file);
        file = -1;
        
        /* sanity checks to verify LFD data is valid */
        lfdHeader = (const LFDHeader *)lfdData;
        if (memcmp(lfdHeader, lfd_id, 4) != 0 || lfdHeader->sheetCount == 0 || lfdHeader->sheetCount > 32) {
            break;
        }
        
        fontBaseSize = sizeof(LFDHeader) + (sizeof(Uint16) * CODE_TABLE_SIZE) + (32 * lfdHeader->sheetCount);
        if (fileSize < (int64_t)(fontBaseSize + (sizeof(LFDCharEntry) * lfdHeader->fontMax))) {
            break;
        }
        
        /* create sprite handle and data */
        fontSprHandle = PL_Handle_AcquireID(DXHANDLE_LUNAFONTSPRITE);
        if (fontSprHandle < 0) {
            break;
        }
        
        fontspr = (LunaFontSprData *)PL_Handle_AllocateData(fontSprHandle, sizeof(LunaFontSprData));
        fontspr->lfdData = lfdData;
        fontspr->lfdHeader = lfdHeader;
        fontspr->lfdIndex = (const Uint16 *)(lfdData + sizeof(LFDHeader));
        fontspr->lfdCharEntries = (const LFDCharEntry *)(lfdData + fontBaseSize);
        fontspr->charMax = lfdHeader->fontMax;
        
        fontspr->sheetCount = lfdHeader->sheetCount;
        fontspr->sheetGraphs = (int *)DXALLOC(sizeof(int) * fontspr->sheetCount);
        fontspr->sheetSprites = (int *)DXALLOC(sizeof(int) * fontspr->sheetCount);
        
        fontspr->space = 0;
        
        for (i = 0; i < fontspr->sheetCount; ++i) {
            char buf[64];
            memcpy(buf, lfdData + sizeof(LFDHeader) + (sizeof(Uint16) * CODE_TABLE_SIZE) + (32 * i), 32);
            buf[32] = '\0';
            if (pExt != NULL) {
                char newBuf[2048];
                PL_Text_ConvertStrncpy(
                    newBuf, -1, buf, g_lunaUseCharSet, 2048);
                PL_Text_ConvertStrncat(
                    newBuf, -1, pExt, g_lunaUseCharSet, 2048);
                
                int surface = PL_Surface_Load(newBuf);
                fontspr->sheetGraphs[i] = PL_Surface_ToTexture(surface);
                PL_Surface_Delete(surface);
            } else {
                /* FIXME: LAG support */
                fontspr->sheetGraphs[i] = -1;
            }
            fontspr->sheetSprites[i] =
                LunaSprite::Create(Num, PRIM_VERTEX_UV1, IsSortZ);
            
            LunaSprite::AttatchTexture(fontspr->sheetSprites[i], 0, fontspr->sheetGraphs[i]);
        }
        
        return fontSprHandle;
    } while(0);
    
    PL_File_Close(file);
    if (lfdData != NULL) {
        DXFREE(lfdData);
    }
        
    return INVALID_FONTSPRITE;
}