Changeset 639


Ignore:
Timestamp:
Feb 12, 2011, 12:53:45 AM (10 years ago)
Author:
sam
Message:

Make font objects use the TileSet system and fix the text objects' Z offset.
Fixes tickets #19 and #24.

Location:
trunk
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/monsterz/board.cpp

    r633 r639  
    155155    data->scoretext = new Text(NULL, "monsterz/gfx/font2.png");
    156156    data->scoretext->SetAlign(Text::ALIGN_RIGHT);
    157     data->scoretext->SetPos(int3(624, 432, 1));
     157    data->scoretext->SetPos(int3(624, 432, 20));
    158158    Ticker::Ref(data->scoretext);
    159159    data->score = 0;
  • trunk/monsterz/interface.cpp

    r633 r639  
    104104    {
    105105        int2 m = mouse + int2(-6, 6 - 48);
    106         Scene::GetDefault()->AddTile((data->tiles << 16) | 22, m.x, m.y, 20, 0);
     106        Scene::GetDefault()->AddTile((data->tiles << 16) | 22, m.x, m.y, 30, 0);
    107107    }
    108108}
  • trunk/monsterz/thumbs.cpp

    r633 r639  
    8686        data->text[n] = new Text(NULL, "monsterz/gfx/font1.png");
    8787        Ticker::Ref(data->text[n]);
    88         int3 p = int3(492, 374 - 27 * n, 1);
     88        int3 p = int3(492, 374 - 27 * n, 20);
    8989        data->text[n]->SetPos(p);
    9090    }
  • trunk/src/font.cpp

    r328 r639  
    1313#endif
    1414
    15 #ifdef WIN32
    16 #   define WIN32_LEAN_AND_MEAN
    17 #   include <windows.h>
    18 #endif
    19 #if defined __APPLE__ && defined __MACH__
    20 #   include <OpenGL/gl.h>
    21 #else
    22 #   define GL_GLEXT_PROTOTYPES
    23 #   include <GL/gl.h>
    24 #endif
    25 
    26 #include <SDL.h>
    27 #include <SDL_image.h>
     15#include <string.h>
     16#include <stdio.h>
    2817
    2918#include "core.h"
     
    3928private:
    4029    char *name;
    41 
    42     SDL_Surface *img;
     30    int tiler;
    4331    int2 size;
    44     float tx, ty;
    45     GLuint texture;
    4632};
    4733
     
    5339  : data(new FontData())
    5440{
    55     data->name = strdup(path);
    56     data->img = NULL;
     41    data->name = (char *)malloc(7 + strlen(path) + 1);
     42    sprintf(data->name, "<font> %s", path);
    5743
    58     for (char const *name = path; *name; name++)
    59         if ((data->img = IMG_Load(name)))
    60             break;
    61 
    62     if (!data->img)
    63     {
    64 #if !FINAL_RELEASE
    65         fprintf(stderr, "ERROR: could not load %s\n", path);
    66 #endif
    67         SDL_Quit();
    68         exit(1);
    69     }
    70 
    71     data->size = int2(data->img->w, data->img->h) / 16;
    72     data->tx = (float)data->size.x / PotUp(data->img->w);
    73     data->ty = (float)data->size.y / PotUp(data->img->h);
     44    data->tiler = Tiler::Register(path, 0, 16, 1.0f);
     45    data->size = Tiler::GetSize(data->tiler);
    7446
    7547    drawgroup = DRAWGROUP_BEFORE;
     
    7850Font::~Font()
    7951{
     52    Tiler::Deregister(data->tiler);
     53    free(data->name);
    8054    delete data;
    8155}
     
    8458{
    8559    Entity::TickDraw(deltams);
    86 
    87     if (IsDestroying())
    88     {
    89         if (data->img)
    90             SDL_FreeSurface(data->img);
    91         else
    92             glDeleteTextures(1, &data->texture);
    93     }
    94     else if (data->img)
    95     {
    96         GLuint format = data->img->format->Amask ? GL_RGBA : GL_RGB;
    97         int planes = data->img->format->Amask ? 4 : 3;
    98 
    99         int w = PotUp(data->img->w);
    100         int h = PotUp(data->img->h);
    101 
    102         uint8_t *pixels = (uint8_t *)data->img->pixels;
    103         if (w != data->img->w || h != data->img->h)
    104         {
    105             uint8_t *tmp = (uint8_t *)malloc(planes * w * h);
    106             for (int line = 0; line < data->img->h; line++)
    107                 memcpy(tmp + planes * w * line,
    108                        pixels + planes * data->img->w * line,
    109                        planes * data->img->w);
    110             pixels = tmp;
    111         }
    112 
    113         glGenTextures(1, &data->texture);
    114         glBindTexture(GL_TEXTURE_2D, data->texture);
    115 
    116         glTexImage2D(GL_TEXTURE_2D, 0, planes, w, h, 0,
    117                      format, GL_UNSIGNED_BYTE, pixels);
    118 
    119         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    120         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    121 
    122         if (pixels != data->img->pixels)
    123             free(pixels);
    124         SDL_FreeSurface(data->img);
    125         data->img = NULL;
    126     }
    12760}
    12861
     
    13467void Font::Print(int3 pos, char const *str)
    13568{
    136     if (data->img)
    137         return;
     69    Scene *scene = Scene::GetDefault();
    13870
    139     glBindTexture(GL_TEXTURE_2D, data->texture);
    140     glBegin(GL_QUADS);
    14171    while (*str)
    14272    {
    14373        uint32_t ch = (uint8_t)*str++;
    144         float tx = data->tx * (ch & 0xf);
    145         float ty = data->ty * ((ch >> 4) & 0xf);
    14674
    14775        if (ch != ' ')
    148         {
    149             glTexCoord2f(tx, ty + data->ty);
    150             glVertex2f(pos.x, pos.y);
    151             glTexCoord2f(tx + data->tx, ty + data->ty);
    152             glVertex2f(pos.x + data->size.x, pos.y);
    153             glTexCoord2f(tx + data->tx, ty);
    154             glVertex2f(pos.x + data->size.x, pos.y + data->size.y);
    155             glTexCoord2f(tx, ty);
    156             glVertex2f(pos.x, pos.y + data->size.y);
    157         }
     76            scene->AddTile((data->tiler << 16) | (ch & 255),
     77                           pos.x, pos.y, pos.z, 0);
    15878
    15979        pos.x += data->size.x;
    16080    }
    161     glEnd();
    162 }
    163 
    164 void Font::PrintBold(int3 pos, char const *str)
    165 {
    166     static struct { int dx, dy; float r, g, b; } tab[] =
    167     {
    168         { -1,  0, 0.0, 0.0, 0.0 },
    169         {  0, -1, 0.0, 0.0, 0.0 },
    170         {  0,  1, 0.0, 0.0, 0.0 },
    171         {  1, -1, 0.0, 0.0, 0.0 },
    172         {  1,  1, 0.0, 0.0, 0.0 },
    173         {  2,  0, 0.0, 0.0, 0.0 },
    174         {  1,  0, 0.5, 0.5, 0.5 },
    175         {  0,  0, 1.0, 1.0, 1.0 },
    176     };
    177 
    178     if (data->img)
    179         return;
    180 
    181     glPushAttrib(GL_COLOR_BUFFER_BIT | GL_CURRENT_BIT);
    182     for (unsigned int i = 0; i < sizeof(tab) / sizeof(*tab); i++)
    183     {
    184         glColor3f(tab[i].r, tab[i].g, tab[i].b);
    185         Print(pos + int3(tab[i].dx, tab[i].dy, 0), str);
    186     }
    187     glPopAttrib();
    18881}
    18982
  • trunk/src/font.h

    r326 r639  
    3535    /* New methods */
    3636    void Print(int3 pos, char const *str);
    37     void PrintBold(int3 pos, char const *str);
    3837    int2 GetSize() const;
    3938
  • trunk/src/ticker.cpp

    r620 r639  
    316316    }
    317317
     318    Scene::GetDefault()->Render();
    318319    Scene::Reset();
    319320
Note: See TracChangeset for help on using the changeset viewer.