Changeset 14


Ignore:
Timestamp:
Jun 27, 2010, 9:57:26 PM (13 years ago)
Author:
sam
Message:

Testing object layers.

Location:
trunk
Files:
1 added
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/test-map.cpp

    r13 r14  
    77#if defined __APPLE__ && defined __MACH__
    88#   include <OpenGL/gl.h>
    9 #   include <OpenGL/glu.h>
    109#else
     10#   define GL_GLEXT_PROTOTYPES
    1111#   include <GL/gl.h>
    12 #   include <GL/glu.h>
     12#   include <GL/glext.h>
    1313#endif
    1414
     
    1616#include <SDL_image.h>
    1717
    18 float xrot, yrot, zrot;
     18#include <math.h>
     19
     20int frames;
    1921
    2022/* Storage for one texture  */
    2123GLuint texture[1];
     24
     25/* Storage for 3 vertex buffers */
     26GLuint buflist[3];
    2227
    2328// Load Bitmaps And Convert To Textures
     
    3540    glBindTexture(GL_TEXTURE_2D, texture[0]);
    3641
    37     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    38     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    39 
    4042    glTexImage2D(GL_TEXTURE_2D, 0, 4, image1->w, image1->h, 0,
    4143                 GL_RGBA, GL_UNSIGNED_BYTE, image1->pixels);
     44
     45    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
     46    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
    4247};
    4348
     49void MakeVBOs(void)
     50{
     51    glGenBuffers(3, buflist);
     52
     53    float vertices[8 * 20 * 15];
     54    for (int y = 0; y < 15; y++)
     55        for (int x = 0; x < 20; x++)
     56        {
     57            vertices[8 * (20 * y + x) + 0] = x * 32;
     58            vertices[8 * (20 * y + x) + 1] = y * 32;
     59            vertices[8 * (20 * y + x) + 2] = x * 32 + 32;
     60            vertices[8 * (20 * y + x) + 3] = y * 32;
     61            vertices[8 * (20 * y + x) + 4] = x * 32 + 32;
     62            vertices[8 * (20 * y + x) + 5] = y * 32 + 32;
     63            vertices[8 * (20 * y + x) + 6] = x * 32;
     64            vertices[8 * (20 * y + x) + 7] = y * 32 + 32;
     65        }
     66    glBindBuffer(GL_ARRAY_BUFFER, buflist[0]);
     67    glBufferData(GL_ARRAY_BUFFER,
     68                 8 * 20 * 15 * sizeof(float), vertices, GL_STATIC_DRAW);
     69
     70    int indices[4 * 20 * 15];
     71    for (int n = 0; n < 4 * 20 * 15; n++)
     72        indices[n] = n;
     73    glBindBuffer(GL_ARRAY_BUFFER, buflist[2]);
     74    glBufferData(GL_ARRAY_BUFFER,
     75                 4 * 20 * 15 * sizeof(int), indices, GL_STATIC_DRAW);
     76}
     77
    4478void InitGL(int Width, int Height)
    4579{
     80    // Resize method
    4681    glViewport(0, 0, Width, Height);
    47     LoadGLTextures();
    48     glEnable(GL_TEXTURE_2D);
    49     glClearColor(0.0f, 0.0f, 1.0f, 0.0f);
    50     glClearDepth(1.0);
    51     glDepthFunc(GL_LESS);
    52     glEnable(GL_DEPTH_TEST);
    53     glShadeModel(GL_SMOOTH);
    54 
    5582    glMatrixMode(GL_PROJECTION);
    5683    glLoadIdentity();
    57 
    58     gluPerspective(45.0f, (GLfloat)Width / (GLfloat)Height, 0.1f, 100.0f);
    59 
     84    glOrtho(0, Width, Height, 0, -1, 10);
    6085    glMatrixMode(GL_MODELVIEW);
     86    glLoadIdentity();
     87
     88    // Init method
     89    glEnable(GL_TEXTURE_2D);
     90    LoadGLTextures();
     91    MakeVBOs();
     92    glShadeModel(GL_SMOOTH);
     93    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
     94    glClearDepth(1.0);
     95    glEnable(GL_DEPTH_TEST);
     96    glDepthFunc(GL_LEQUAL);
     97    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
     98
     99    glEnable(GL_BLEND);
     100    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     101}
     102
     103void PutMap(int const *themap)
     104{
     105    // Put map
     106    float uvs[8 * 20 * 15];
     107
     108    for (int y = 0; y < 15; y++)
     109        for (int x = 0; x < 20; x++)
     110        {
     111            int tile = themap[20 * y + x];
     112            float ty = .0625f * (tile / 16);
     113            float tx = .0625f * (tile % 16);
     114            uvs[8 * (20 * y + x) + 0] = tx;
     115            uvs[8 * (20 * y + x) + 1] = ty;
     116            uvs[8 * (20 * y + x) + 2] = tx + .0625f;
     117            uvs[8 * (20 * y + x) + 3] = ty;
     118            uvs[8 * (20 * y + x) + 4] = tx + .0625f;
     119            uvs[8 * (20 * y + x) + 5] = ty + .0625f;
     120            uvs[8 * (20 * y + x) + 6] = tx;
     121            uvs[8 * (20 * y + x) + 7] = ty + .0625f;
     122        }
     123    glBindBuffer(GL_ARRAY_BUFFER, buflist[1]);
     124    glBufferData(GL_ARRAY_BUFFER,
     125                 8 * 20 * 15 * sizeof(float), uvs, GL_STATIC_DRAW);
     126
     127    glEnableClientState(GL_VERTEX_ARRAY);
     128    glEnableClientState(GL_TEXTURE_COORD_ARRAY);
     129    glEnableClientState(GL_INDEX_ARRAY);
     130
     131    glBindTexture(GL_TEXTURE_2D, texture[0]);
     132
     133    glBindBuffer(GL_ARRAY_BUFFER, buflist[0]);
     134    glVertexPointer(2, GL_FLOAT, 0, NULL);
     135    glBindBuffer(GL_ARRAY_BUFFER, buflist[1]);
     136    glTexCoordPointer(2, GL_FLOAT, 0, NULL);
     137    glBindBuffer(GL_ARRAY_BUFFER, buflist[2]);
     138    glIndexPointer(GL_INT, 0, NULL);
     139
     140    glDrawArrays(GL_QUADS, 0, 4 * 20 * 15);
     141
     142    glDisableClientState(GL_VERTEX_ARRAY);
     143    glDisableClientState(GL_TEXTURE_COORD_ARRAY);
     144    glDisableClientState(GL_INDEX_ARRAY);
    61145}
    62146
    63147/* The main drawing function. */
    64 void DrawGLScene()
    65 {
    66     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
     148void DrawScene()
     149{
     150    int ground[20 * 15] =
     151    {
     15218, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
     15318,  1,  2,  2,  2, 34,  2,  2,  2,  2,  2,  2,  3, 34,  4, 18, 18, 18, 18, 18,
     15418, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 20,  4, 18, 18, 18, 18,
     15518, 19, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 19, 18, 18, 18, 18,
     15618, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 17, 17, 18, 18, 18, 18,
     15718, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 18, 20, 36, 18, 18, 18, 18,
     15818, 33,  2,  2,  2,  2,  2,  2,  2,  2, 34,  2, 35,  2, 36, 18, 18, 18, 18, 18,
     15918, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18,
     16016, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
     16116, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
     16216, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
     16316, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
     16416, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
     16516, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
     16616, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16,
     167    };
     168
     169    int l1objects[20 * 15] =
     170    {
     171 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     172 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     173 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     174 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     175 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 49,  0,  0,  0,  0,  0,  0,  0,  0,
     176 0,  0,  0,  0, 49, 49,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     177 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     178 0,  0,  0,  0, 49, 49, 49, 49, 49, 49,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     179 0,  0,  0,  0,  0, 49, 49, 49, 49,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     180 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 48,  0, 32, 49,  0,  0,  0,
     181 0,  0,  0, 49, 49, 32,  0, 50,  0,  0,  0, 48,  0, 64,  0, 49, 49,  0,  0,  0,
     182 0,  0,  0,  0,  0,  0,  0,  0,  0, 32,  0, 64,  0,  0,  0,  0,  0,  0,  0,  0,
     183 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0, 32,  0,  0,  0,  0,  0,
     184 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     185 0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,  0,
     186    };
     187
     188    glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
    67189    glLoadIdentity();
    68190
    69     glTranslatef(0.0f, 0.0f, -5.0f);
    70 
    71     glRotatef(xrot, 1.0f, 0.0f, 0.0f);
    72     glRotatef(yrot, 0.0f, 1.0f, 0.0f);
    73     glRotatef(zrot, 0.0f, 0.0f, 1.0f);
    74 
    75     glBindTexture(GL_TEXTURE_2D, texture[0]);
    76 
    77     glBegin(GL_QUADS);
    78 
    79     glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
    80     glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
    81     glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
    82     glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
    83 
    84     glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
    85     glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
    86     glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
    87     glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
    88 
    89     glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
    90     glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
    91     glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
    92     glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
    93 
    94     glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
    95     glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
    96     glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
    97     glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
    98 
    99     glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, -1.0f);
    100     glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f,  1.0f, -1.0f);
    101     glTexCoord2f(0.0f, 1.0f); glVertex3f( 1.0f,  1.0f,  1.0f);
    102     glTexCoord2f(0.0f, 0.0f); glVertex3f( 1.0f, -1.0f,  1.0f);
    103 
    104     glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, -1.0f);
    105     glTexCoord2f(1.0f, 0.0f); glVertex3f(-1.0f, -1.0f,  1.0f);
    106     glTexCoord2f(1.0f, 1.0f); glVertex3f(-1.0f,  1.0f,  1.0f);
    107     glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f,  1.0f, -1.0f);
    108 
    109     glEnd();
    110 
    111     xrot += 0.2f;
    112     yrot += 0.2f;
    113     zrot += 0.2f;
     191    PutMap(ground);
     192    //glTranslatef(10.0f * sinf(0.16f * frames), 10.0f * cosf(0.16f * frames), 0.0f);
     193    PutMap(l1objects);
    114194
    115195    SDL_GL_SwapBuffers();
     
    121201
    122202  /* Initialize SDL for video output */
    123   if ( SDL_Init(SDL_INIT_VIDEO) < 0 )
     203  if (SDL_Init(SDL_INIT_VIDEO) < 0)
    124204  {
    125205    fprintf(stderr, "Unable to initialize SDL: %s\n", SDL_GetError());
     
    128208
    129209  /* Create a 640x480 OpenGL screen */
    130   if ( SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL )
     210  if (SDL_SetVideoMode(640, 480, 0, SDL_OPENGL) == NULL)
    131211  {
    132212    fprintf(stderr, "Unable to create OpenGL screen: %s\n", SDL_GetError());
     
    141221  InitGL(640, 480);
    142222  done = 0;
    143   while ( !done )
     223  frames = 0;
     224  Uint32 ticks = SDL_GetTicks();
     225  Uint32 start = ticks;
     226  while (!done)
    144227  {
    145     DrawGLScene();
     228    DrawScene();
     229    frames++;
    146230
    147231    /* This could go in a separate function */
    148232    SDL_Event event;
    149     while ( SDL_PollEvent(&event) )
    150     {
    151       if ( event.type == SDL_QUIT )
     233    while (SDL_PollEvent(&event))
     234    {
     235      if (event.type == SDL_QUIT)
    152236        done = 1;
    153       if ( event.type == SDL_KEYDOWN )
    154         if ( event.key.keysym.sym == SDLK_ESCAPE )
     237      if (event.type == SDL_KEYDOWN)
     238        if (event.key.keysym.sym == SDLK_ESCAPE)
    155239          done = 1;
    156240    }
     241
     242    while (SDL_GetTicks() < ticks + 33)
     243        SDL_Delay(1);
     244    ticks = SDL_GetTicks();
    157245  }
     246  printf("%i fps\n", frames * 1000 / (SDL_GetTicks() - start));
    158247  SDL_Quit();
    159   return 1;
    160 }
    161 
     248
     249  return EXIT_SUCCESS;
     250}
     251
  • trunk/test-map.py

    r12 r14  
    116116
    117117    themap = [
    118         [ 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 ],
    119         [ 17,  0,  1,  1,  1, 33,  1,  1,  1,  1,  1,  1,  2, 33,  3, 17, 17, 17, 17, 17 ],
    120         [ 17, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 16, 17, 19,  3, 17, 17, 17, 17 ],
    121         [ 17, 18, 17, 48, 49, 50, 48, 49, 50, 48, 49, 17, 16, 17, 16, 18, 17, 17, 17, 17 ],
    122         [ 17, 16, 17, 48, 49, 50, 48, 49, 50, 48, 49, 17, 16, 17, 16, 16, 17, 17, 17, 17 ],
    123         [ 17, 16, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 18, 17, 19, 35, 17, 17, 17, 17 ],
    124         [ 17, 32,  1,  1,  1,  1,  1,  1,  1,  1, 33,  1, 34,  1, 35, 17, 17, 17, 17, 17 ],
    125         [ 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17, 17 ],
    126         [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
    127         [ 51, 51, 52, 52, 52, 51, 52, 52, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
    128         [ 51, 51, 52, 52, 52, 51, 51, 52, 51, 52, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
    129         [ 51, 51, 52, 52, 52, 51, 51, 51, 51, 51, 51, 52, 52, 51, 51, 51, 51, 51, 51, 51 ],
    130         [ 51, 51, 52, 52, 52, 51, 51, 51, 51, 51, 51, 52, 52, 51, 51, 51, 51, 51, 51, 51 ],
    131         [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
    132         [ 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51, 51 ],
     118        [ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18 ],
     119        [ 18,  1,  2,  2,  2, 34,  2,  2,  2,  2,  2,  2,  3, 34,  4, 18, 18, 18, 18, 18 ],
     120        [ 18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 17, 18, 20,  4, 18, 18, 18, 18 ],
     121        [ 18, 19, 18, 49, 50, 51, 49, 50, 51, 49, 50, 18, 17, 18, 17, 19, 18, 18, 18, 18 ],
     122        [ 18, 17, 18, 49, 50, 51, 49, 50, 51, 49, 50, 18, 17, 18, 17, 17, 18, 18, 18, 18 ],
     123        [ 18, 17, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 19, 18, 20, 36, 18, 18, 18, 18 ],
     124        [ 18, 33,  2,  2,  2,  2,  2,  2,  2,  2, 34,  2, 35,  2, 36, 18, 18, 18, 18, 18 ],
     125        [ 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18 ],
     126        [ 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52 ],
     127        [ 52, 52, 53, 53, 53, 52, 53, 53, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52 ],
     128        [ 52, 52, 53, 53, 53, 52, 52, 53, 52, 53, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52 ],
     129        [ 52, 52, 53, 53, 53, 52, 52, 52, 52, 52, 52, 53, 53, 52, 52, 52, 52, 52, 52, 52 ],
     130        [ 52, 52, 53, 53, 53, 52, 52, 52, 52, 52, 52, 53, 53, 52, 52, 52, 52, 52, 52, 52 ],
     131        [ 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52 ],
     132        [ 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52, 52 ],
    133133    ]
    134134    glPushMatrix()
Note: See TracChangeset for help on using the changeset viewer.