Changeset 79


Ignore:
Timestamp:
Jul 8, 2010, 2:00:13 AM (10 years ago)
Author:
sam
Message:

Rendering starts to work again. There is no Z-sorting yet.

Location:
trunk/src
Files:
2 added
13 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/Makefile

    r75 r79  
    11
    2 SRC = test-map.cpp video.cpp tiler.cpp tileset.cpp scene.cpp layer.cpp map.cpp
     2SRC = test-map.cpp \
     3      game.cpp video.cpp tiler.cpp tileset.cpp scene.cpp layer.cpp map.cpp
    34
    45all: test-map
  • trunk/src/layer.cpp

    r75 r79  
    44#include "layer.h"
    55
    6 Layer::Layer(int w, int h, int in_z, uint32_t *in_data)
     6Layer::Layer(int w, int h, int z, uint32_t *in_data)
    77{
    88    width = w;
    99    height = h;
    10     z = in_z;
     10    altitude = z;
    1111    data = in_data;
    1212
     
    2727}
    2828
    29 void Layer::Draw()
     29void Layer::Render(Scene *scene, int x, int y, int z)
    3030{
    31     for (int y = 0; y < 32; y++)
    32         for (int x = 0; x < 32; x++)
    33             ;//tileset->AddTile(GetTile(x, y), x * 32, y * 32, z);
     31    for (int j = 0; j < height; j++)
     32        for (int i = 0; i < width; i++)
     33            if (data[j * width + i])
     34                scene->AddTile(data[j * width + i],
     35                               i * 32 - x, j * 32 - y, altitude + z);
    3436}
    3537
    3638int Layer::GetZ()
    3739{
    38     return z;
     40    return altitude;
    3941}
    4042
    41 unsigned int Layer::GetTile(int x, int y)
    42 {
    43     return data[y * width + x];
    44 }
    45 
  • trunk/src/layer.h

    r75 r79  
    77#define __DH_LAYER_H__
    88
    9 #include <cstdio>
    109#include <stdint.h>
     10
     11#include "scene.h"
    1112
    1213class Layer
     
    1718
    1819    int GetZ();
    19     unsigned int GetTile(int x, int y);
    2020
    21     void Draw();
     21    void Render(Scene *scene, int x, int y, int z);
    2222
    2323private:
    24     int width, height, z;
     24    int width, height, altitude;
    2525    uint32_t *data;
    2626};
  • trunk/src/map.cpp

    r76 r79  
    5757        fgets(tmp, BUFSIZ, fp);
    5858
    59         if (tiles)
     59        if (tiles && !strchr(tmp, '<'))
    6060        {
    6161            /* We are in the process of reading layer data. Only stop
     
    9595                data->nlayers++;
    9696                tiles = NULL;
     97                //fprintf(stderr, "new layer %ix%i\n", width, height);
    9798            }
    9899        }
     
    107108            data->tilers[data->ntilers] = Tiler::Register(str);
    108109            data->ntilers++;
     110            //fprintf(stderr, "new tiler %s\n", str);
    109111        }
    110112        else if (sscanf(tmp, " <layer name=\"%c%i%c%*[^\"]\" "
     
    135137}
    136138
    137 void Map::Draw()
     139void Map::Render(Scene *scene, int x, int y, int z)
    138140{
    139141    for (int i = 0; i < data->nlayers; i++)
    140         data->layers[i]->Draw();
     142        data->layers[i]->Render(scene, x, y, z);
    141143}
    142144
  • trunk/src/map.h

    r75 r79  
    77#define __DH_MAP_H__
    88
    9 #include <cstdio>
    10 
    11 #include "layer.h"
     9#include "scene.h"
    1210
    1311class MapData;
     
    1917    ~Map();
    2018
    21     void Draw();
     19    void Render(Scene *scene, int x, int y, int z);
    2220
    2321private:
  • trunk/src/scene.cpp

    r75 r79  
     1
     2#include <stdlib.h>
    13
    24#include "scene.h"
     5#include "tiler.h"
     6
     7struct Tile
     8{
     9    uint32_t prio, code;
     10    int x, y;
     11};
    312
    413/*
     
    1120
    1221private:
    13     int dummy;
     22    static int Compare(void const *p1, void const *p2)
     23    {
     24        Tile const *t1 = (Tile const *)p1;
     25        Tile const *t2 = (Tile const *)p2;
     26
     27        return t1->prio - t2->prio;
     28    }
     29
     30    Tile *tiles;
     31    int ntiles;
    1432};
    1533
     
    2139{
    2240    data = new SceneData();
     41    data->tiles = 0;
     42    data->ntiles = 0;
    2343}
    2444
     
    2848}
    2949
     50void Scene::AddTile(uint32_t code, int x, int y, int z)
     51{
     52    if ((data->ntiles % 1024) == 0)
     53        data->tiles = (Tile *)realloc(data->tiles,
     54                                      (data->ntiles + 1024) * sizeof(Tile));
     55    data->tiles[data->ntiles].prio = 0;
     56    data->tiles[data->ntiles].code = code;
     57    data->tiles[data->ntiles].x = x;
     58    data->tiles[data->ntiles].y = y;
     59    data->ntiles++;
     60}
     61
     62void Scene::Render() // XXX: rename to Blit()
     63{
     64    qsort(data->tiles, data->ntiles, sizeof(Tile), SceneData::Compare);
     65
     66    for (int i = 0; i < data->ntiles; i++)
     67        Tiler::Render(data->tiles[i].code, data->tiles[i].x, data->tiles[i].y);
     68
     69    free(data->tiles);
     70    data->tiles = 0;
     71    data->ntiles = 0;
     72}
     73
  • trunk/src/scene.h

    r75 r79  
    77#define __DH_SCENE_H__
    88
    9 #include <cstdio>
    109#include <stdint.h>
    1110
     
    1817    ~Scene();
    1918
     19    void AddTile(uint32_t code, int x, int y, int z);
     20    void Render();
     21
    2022private:
    2123    SceneData *data;
  • trunk/src/test-map.cpp

    r75 r79  
    77
    88#include "video.h"
    9 #include "tiler.h"
    10 #include "map.h"
     9#include "game.h"
    1110
    1211int main(int argc, char **argv)
    1312{
    1413    Video *video = new Video("Deus Hax", 640, 480);
    15     Map *map = new Map("maps/testmap-grass.tmx");
     14    Game *game = new Game("maps/testmap-library.tmx");
    1615
    1716    for (int done = 0; !done; )
     
    1918        video->Clear();
    2019
    21         //map->Draw(tiler);
     20        /* Test stuff */
     21        int mx, my;
     22        SDL_GetMouseState(&mx, &my);
     23        game->SetMouse(mx * (640 - 32) / 640, my * (480 - 32) / 480);
    2224
    23         /* Test stuff */
    24         int playerx, playery;
    25         SDL_GetMouseState(&playerx, &playery);
    26         playerx = playerx * (640 - 32) / 640;
    27         playery = playery * (480 - 32) / 480;
    28 
    29         //tiler->AddTile(50, playerx, playery, 1);
    30 
    31         //tiler->Render();
     25        game->Render();
    3226        video->Refresh(33.33333f);
    3327
     
    4842    }
    4943
    50     delete map;
     44    delete game;
    5145    delete video;
    5246
  • trunk/src/tiler.cpp

    r75 r79  
    8484}
    8585
     86void Tiler::Render(uint32_t code, int x, int y)
     87{
     88    int id = (code >> 16) - 1; /* ID 0 is for the empty tileset */
     89
     90    data->tilesets[id]->BlitTile(code & 0xffff, x, y);
     91}
     92
  • trunk/src/tiler.h

    r75 r79  
    77#define __DH_TILER_H__
    88
     9#include <stdint.h>
     10
    911class Tiler
    1012{
     
    1214    static int Register(char const *path);
    1315    static void Deregister(int id);
     16
     17    static void Render(uint32_t code, int x, int y);
    1418};
    1519
  • trunk/src/tileset.cpp

    r75 r79  
    4242
    4343    GLuint texture[1];
    44     GLuint buflist[3];
    4544};
    4645
     
    7877    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    7978    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    80 
    81     /* Three GPU buffers */
    82     glGenBuffers(3, data->buflist);
    8379}
    8480
     
    8682{
    8783    glDeleteTextures(1, data->texture);
    88     glDeleteBuffers(3, data->buflist);
    8984
    9085    free(data->tiles);
     
    108103}
    109104
    110 void TileSet::AddTile(int n, int x, int y, int z)
     105void TileSet::BlitTile(uint32_t id, int x, int y)
    111106{
    112     if ((data->ntiles % 1024) == 0)
    113     {
    114         data->tiles = (int *)realloc(data->tiles,
    115                                      (data->ntiles + 1024) * 4 * sizeof(int));
    116     }
     107    float tx = .0625f * (id & 0xf);
     108    float ty = .0625f * ((id >> 4) & 0xf);
    117109
    118     data->tiles[4 * data->ntiles] = n;
    119     data->tiles[4 * data->ntiles + 1] = x;
    120     data->tiles[4 * data->ntiles + 2] = y;
    121     data->tiles[4 * data->ntiles + 3] = z;
    122 
    123     data->ntiles++;
     110    glBindTexture(GL_TEXTURE_2D, data->texture[0]);
     111    glBegin(GL_QUADS);
     112        glTexCoord2f(tx, ty);
     113        glVertex2f(x, y);
     114        glTexCoord2f(tx + .0625f, ty);
     115        glVertex2f(x + 32, y);
     116        glTexCoord2f(tx + .0625f, ty + .0625f);
     117        glVertex2f(x + 32, y + 32);
     118        glTexCoord2f(tx, ty + .0625f);
     119        glVertex2f(x, y + 32);
     120    glEnd();
    124121}
    125122
    126 void TileSet::Render()
    127 {
    128     /* Sort tiles */
    129     qsort(data->tiles, data->ntiles, 4 * sizeof(int), TileSetData::Compare);
    130 
    131     /* Texture coord buffer */
    132     float uvs[8 * data->ntiles];
    133     for (int n = 0; n < data->ntiles; n++)
    134     {
    135         int tile = data->tiles[4 * n];
    136         float ty = .0625f * (tile / 16);
    137         float tx = .0625f * (tile % 16);
    138         uvs[8 * n + 0] = tx;
    139         uvs[8 * n + 1] = ty;
    140         uvs[8 * n + 2] = tx + .0625f;
    141         uvs[8 * n + 3] = ty;
    142         uvs[8 * n + 4] = tx + .0625f;
    143         uvs[8 * n + 5] = ty + .0625f;
    144         uvs[8 * n + 6] = tx;
    145         uvs[8 * n + 7] = ty + .0625f;
    146     }
    147     glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
    148     glBufferData(GL_ARRAY_BUFFER,
    149                  8 * data->ntiles * sizeof(float), uvs, GL_STATIC_DRAW);
    150 
    151     /* Vertex buffer */
    152     float vertices[8 * data->ntiles];
    153     for (int n = 0; n < data->ntiles; n++)
    154     {
    155         int x = data->tiles[4 * n + 1];
    156         int y = data->tiles[4 * n + 2];
    157         vertices[8 * n + 0] = x;
    158         vertices[8 * n + 1] = y;
    159         vertices[8 * n + 2] = x + 32;
    160         vertices[8 * n + 3] = y;
    161         vertices[8 * n + 4] = x + 32;
    162         vertices[8 * n + 5] = y + 32;
    163         vertices[8 * n + 6] = x;
    164         vertices[8 * n + 7] = y + 32;
    165     }
    166     glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
    167     glBufferData(GL_ARRAY_BUFFER, 8 * data->ntiles * sizeof(float),
    168                  vertices, GL_STATIC_DRAW);
    169 
    170     /* Index buffer */
    171     int indices[4 * data->ntiles];
    172     for (int n = 0; n < 4 * data->ntiles; n++)
    173         indices[n] = n;
    174     glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
    175     glBufferData(GL_ARRAY_BUFFER, 4 * data->ntiles * sizeof(int),
    176                  indices, GL_STATIC_DRAW);
    177 
    178     /* Draw everything */
    179     glEnableClientState(GL_VERTEX_ARRAY);
    180     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    181     glEnableClientState(GL_INDEX_ARRAY);
    182 
    183     glBindTexture(GL_TEXTURE_2D, data->texture[0]);
    184 
    185     glBindBuffer(GL_ARRAY_BUFFER, data->buflist[0]);
    186     glVertexPointer(2, GL_FLOAT, 0, NULL);
    187     glBindBuffer(GL_ARRAY_BUFFER, data->buflist[1]);
    188     glTexCoordPointer(2, GL_FLOAT, 0, NULL);
    189     glBindBuffer(GL_ARRAY_BUFFER, data->buflist[2]);
    190     glIndexPointer(GL_INT, 0, NULL);
    191 
    192     glDrawArrays(GL_QUADS, 0, 4 * data->ntiles);
    193 
    194     glDisableClientState(GL_VERTEX_ARRAY);
    195     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    196     glDisableClientState(GL_INDEX_ARRAY);
    197 
    198     /* Empty our shit */
    199     free(data->tiles);
    200     data->tiles = NULL;
    201     data->ntiles = 0;
    202 }
    203 
  • trunk/src/tileset.h

    r75 r79  
    66#if !defined __DH_TILESET_H__
    77#define __DH_TILESET_H__
     8
     9#include <stdint.h>
    810
    911class TileSetData;
     
    2022    char const *GetName();
    2123
    22     void AddTile(int n, int x, int y, int z);
    23 
    24     void Render();
     24    void BlitTile(uint32_t id, int x, int y);
    2525
    2626private:
  • trunk/src/video.cpp

    r74 r79  
    5555    SDL_WM_SetCaption(title, NULL);
    5656    SDL_ShowCursor(0);
    57     //SDL_WM_GrabInput(SDL_GRAB_ON);
     57    SDL_WM_GrabInput(SDL_GRAB_ON);
    5858
    5959    /* Initialise OpenGL */
Note: See TracChangeset for help on using the changeset viewer.