Changeset 22


Ignore:
Timestamp:
Jun 30, 2010, 3:06:14 PM (13 years ago)
Author:
sam
Message:

Create a tile manager. There are no longer any GL calls in test-map.cpp or
in public headers. Good cleanup.

Location:
trunk/src
Files:
2 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/Makefile

    r20 r22  
    11
    2 SRC = test-map.cpp video.cpp
     2SRC = test-map.cpp video.cpp tiler.cpp
    33
    44all: test-map
  • trunk/src/test-map.cpp

    r21 r22  
    11// Test stuff
    22
    3 #ifdef WIN32
    4 #   define WIN32_LEAN_AND_MEAN
    5 #   include <windows.h>
    6 #endif
    7 #if defined __APPLE__ && defined __MACH__
    8 #   include <OpenGL/gl.h>
    9 #else
    10 #   define GL_GLEXT_PROTOTYPES
    11 #   include <GL/gl.h>
    12 #   include <GL/glext.h>
    13 #endif
    14 
    153#include <SDL.h>
    16 #include <SDL_image.h>
    174
    185#include <stdio.h>
     
    207
    218#include "video.h"
     9#include "tiler.h"
    2210
    23 /* Storage for one texture  */
    24 GLuint texture[1];
    25 
    26 /* Storage for 3 vertex buffers */
    27 GLuint buflist[3];
     11/* Global objects */
     12Video *video;
     13Tiler *tiler;
    2814
    2915/* Storage for map layers */
     
    3117int width = 32, height = 32;
    3218int nlayers = 0;
    33 
    34 /* Player coordinates */
    35 int playerx = 0, playery = 0;
    36 
    37 // Load Bitmaps And Convert To Textures
    38 void LoadGLTextures(void)
    39 {       
    40     SDL_Surface *image1 = IMG_Load("art/test/groundtest.png");
    41 
    42     if (!image1)
    43     {
    44         SDL_Quit();
    45         exit(1);
    46     }
    47 
    48     glGenTextures(1, &texture[0]);
    49     glBindTexture(GL_TEXTURE_2D, texture[0]);
    50 
    51     glTexImage2D(GL_TEXTURE_2D, 0, 4, image1->w, image1->h, 0,
    52                  GL_RGBA, GL_UNSIGNED_BYTE, image1->pixels);
    53 
    54     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
    55     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    56 };
    57 
    58 void MakeVBOs(void)
    59 {
    60     glGenBuffers(3, buflist);
    61 }
    6219
    6320void LoadMap(void)
     
    8542}
    8643
    87 void PutMap(int const *themap, int width, int height)
    88 {
    89     // Put map
    90     float uvs[8 * width * height];
    91 
    92     for (int y = 0; y < height; y++)
    93         for (int x = 0; x < width; x++)
    94         {
    95             int tile = themap[width * y + x];
    96             float ty = .0625f * (tile / 16);
    97             float tx = .0625f * (tile % 16);
    98             uvs[8 * (width * y + x) + 0] = tx;
    99             uvs[8 * (width * y + x) + 1] = ty;
    100             uvs[8 * (width * y + x) + 2] = tx + .0625f;
    101             uvs[8 * (width * y + x) + 3] = ty;
    102             uvs[8 * (width * y + x) + 4] = tx + .0625f;
    103             uvs[8 * (width * y + x) + 5] = ty + .0625f;
    104             uvs[8 * (width * y + x) + 6] = tx;
    105             uvs[8 * (width * y + x) + 7] = ty + .0625f;
    106         }
    107     glBindBuffer(GL_ARRAY_BUFFER, buflist[1]);
    108     glBufferData(GL_ARRAY_BUFFER,
    109                  8 * width * height * sizeof(float), uvs, GL_STATIC_DRAW);
    110 
    111     float vertices[8 * width * height];
    112     for (int y = 0; y < height; y++)
    113         for (int x = 0; x < width; x++)
    114         {
    115             vertices[8 * (width * y + x) + 0] = x * 32;
    116             vertices[8 * (width * y + x) + 1] = y * 32;
    117             vertices[8 * (width * y + x) + 2] = x * 32 + 32;
    118             vertices[8 * (width * y + x) + 3] = y * 32;
    119             vertices[8 * (width * y + x) + 4] = x * 32 + 32;
    120             vertices[8 * (width * y + x) + 5] = y * 32 + 32;
    121             vertices[8 * (width * y + x) + 6] = x * 32;
    122             vertices[8 * (width * y + x) + 7] = y * 32 + 32;
    123         }
    124     glBindBuffer(GL_ARRAY_BUFFER, buflist[0]);
    125     glBufferData(GL_ARRAY_BUFFER,
    126                  8 * width * height * sizeof(float), vertices, GL_STATIC_DRAW);
    127 
    128     int indices[4 * width * height];
    129     for (int n = 0; n < 4 * width * height; n++)
    130         indices[n] = n;
    131     glBindBuffer(GL_ARRAY_BUFFER, buflist[2]);
    132     glBufferData(GL_ARRAY_BUFFER,
    133                  4 * width * height * sizeof(int), indices, GL_STATIC_DRAW);
    134 
    135     glEnableClientState(GL_VERTEX_ARRAY);
    136     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
    137     glEnableClientState(GL_INDEX_ARRAY);
    138 
    139     glBindTexture(GL_TEXTURE_2D, texture[0]);
    140 
    141     glBindBuffer(GL_ARRAY_BUFFER, buflist[0]);
    142     glVertexPointer(2, GL_FLOAT, 0, NULL);
    143     glBindBuffer(GL_ARRAY_BUFFER, buflist[1]);
    144     glTexCoordPointer(2, GL_FLOAT, 0, NULL);
    145     glBindBuffer(GL_ARRAY_BUFFER, buflist[2]);
    146     glIndexPointer(GL_INT, 0, NULL);
    147 
    148     glDrawArrays(GL_QUADS, 0, 4 * width * height);
    149 
    150     glDisableClientState(GL_VERTEX_ARRAY);
    151     glDisableClientState(GL_TEXTURE_COORD_ARRAY);
    152     glDisableClientState(GL_INDEX_ARRAY);
    153 }
    154 
    15544/* The main drawing function. */
    15645void DrawScene()
    15746{
    158 /*
    159     int ground[20 * 15] =
    160     {
    161 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
    162 18,  1,  2,  2,  2, 34,  2,  2,  2,  2,  2,  2,  3, 34,  4, 18, 18, 18, 18, 18,
    163 18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 20,  4, 18, 18, 18, 18,
    164 18, 19, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 19, 18, 18, 18, 18,
    165 18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 17, 18, 18, 18, 18,
    166 18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 18, 20, 36, 18, 18, 18, 18,
    167 18, 33,  2,  2,  2,  2,  2,  2,  2,  2, 34,  2, 35,  2, 36, 18, 18, 18, 18, 18,
    168 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
    169 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    170 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    171 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    172 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    173 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    174 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    175 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
    176     };
    177 
    178     int l1objects[20 * 15] =
    179     {
    180  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    181  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    182  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    183  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    184  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 49,  0,  0,  0,  0,  0,  0,  0,  0,
    185  0,  0,  0,  0, 49, 49,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    186  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    187  0,  0,  0,  0, 49, 49, 49, 49, 49, 49,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    188  0,  0,  0,  0,  0, 49, 49, 49, 49,  0,  0,  0,  0, 25,  9,  9,  9,  8,  0,  0,
    189  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 54, 40, 39, 39, 41,  0,  0,
    190  0,  0,  0, 49, 49, 32,  0, 50,  0,  0,  0, 48,  0, 24, 23, 54, 40, 55,  0,  0,
    191  0,  0,  0,  0,  0,  0,  0,  0,  0, 32,  0, 64,  0, 24, 23, 24, 23,  7,  0,  0,
    192  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  6, 38, 24, 23,  7,  0,  0,
    193  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  6, 38, 22,  0,  0,
    194  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
    195     };
    196 */
    197 
    198     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    199     glLoadIdentity();
     47    video->Clear();
    20048
    20149    for (int i = 0; i < nlayers; i++)
    202     {
    203         glPushMatrix();
    204         if (i == 2)
    205             glTranslatef(playerx, playery, 0.0f);
    206         PutMap(layers[i], width, height);
    207         glPopMatrix();
    208     }
     50        for (int y = 0; y < height; y++)
     51            for (int x = 0; x < width; x++)
     52                tiler->AddTile(layers[i][y * width + x], x * 32, y * 32, i);
     53
     54    /* Test stuff */
     55    int playerx, playery;
     56    SDL_GetMouseState(&playerx, &playery);
     57    tiler->AddTile(50, playerx, playery, nlayers);
     58
     59    tiler->Render();
     60    video->Refresh(33.33333f);
    20961}
    21062
    21163int main(int argc, char **argv)
    21264{
    213     Video *video = new Video("Deus Hax", 640, 480);
     65    video = new Video("Deus Hax", 640, 480);
     66    tiler = new Tiler();
    21467
    21568    int done;
    21669
    21770    /* Loop, drawing and checking events */
    218     LoadGLTextures();
    219     MakeVBOs();
    22071    LoadMap();
    22172
     
    22475    {
    22576        DrawScene();
    226 
    227         video->Refresh(33.33333f);
    228 
    229         SDL_GetMouseState(&playerx, &playery);
    23077
    23178        /* This could go in a separate function */
     
    24592    }
    24693
     94    delete tiler;
    24795    delete video;
    24896
  • trunk/src/video.cpp

    r21 r22  
    9191}
    9292
     93void Video::Clear()
     94{
     95    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     96    glLoadIdentity();
     97}
     98
    9399void Video::Refresh(float milliseconds)
    94100{
  • trunk/src/video.h

    r18 r22  
    1010    int GetWidth() const;
    1111    int GetHeight() const;
     12    void Clear();
    1213    void Refresh(float milliseconds);
    1314    void FullScreen();
Note: See TracChangeset for help on using the changeset viewer.