浏览代码

Game is singleton. Started initializing Render.

Modified Camera and Render.
Thomas Buck 10 年前
父节点
当前提交
23c9159d52
共有 7 个文件被更改,包括 102 次插入192 次删除
  1. 15
    2
      include/Game.h
  2. 5
    5
      include/OpenRaider.h
  3. 0
    14
      include/Render.h
  4. 55
    7
      src/Game.cpp
  5. 12
    19
      src/OpenRaider.cpp
  6. 12
    142
      src/Render.cpp
  7. 3
    3
      src/Window.cpp

+ 15
- 2
include/Game.h 查看文件

@@ -8,7 +8,9 @@
8 8
 #ifndef _GAME_H_
9 9
 #define _GAME_H_
10 10
 
11
+#include "Camera.h"
11 12
 #include "global.h"
13
+#include "Render.h"
12 14
 #include "TombRaider.h"
13 15
 #include "World.h"
14 16
 
@@ -18,11 +20,16 @@
18 20
 class Game {
19 21
 public:
20 22
 
21
-    // Throw exception with negative integer error code if fails
22
-    Game(const char *level);
23
+    Game();
23 24
 
24 25
     ~Game();
25 26
 
27
+    int initialize();
28
+
29
+    int loadLevel(const char *level);
30
+
31
+    void destroy();
32
+
26 33
     void handleAction(ActionEvents action, bool isFinished);
27 34
 
28 35
     void handleMouseMotion(int xrel, int yrel);
@@ -33,12 +40,18 @@ public:
33 40
 
34 41
     World mWorld;
35 42
     entity_t *mLara;
43
+    Render *mRender;
44
+    Camera *mCamera;
36 45
 
37 46
 private:
38 47
 
48
+    bool mLoaded;
39 49
     char *mName;
40 50
     TombRaider mTombRaider;
41 51
 
52
+    unsigned int mTextureStart;
53
+    unsigned int mTextureLevelOffset;
54
+    unsigned int mTextureOffset;
42 55
 };
43 56
 
44 57
 #endif

+ 5
- 5
include/OpenRaider.h 查看文件

@@ -67,6 +67,11 @@ public:
67 67
     float mCameraRotationDeltaX;
68 68
     float mCameraRotationDeltaY;
69 69
 
70
+    char *mBaseDir;
71
+    char *mPakDir;
72
+    char *mAudioDir;
73
+    char *mDataDir;
74
+
70 75
 private:
71 76
 
72 77
     int command(const char *command, std::vector<char *> *args);
@@ -88,11 +93,6 @@ private:
88 93
     bool mInit;
89 94
     bool mRunning;
90 95
 
91
-    char *mBaseDir;
92
-    char *mPakDir;
93
-    char *mAudioDir;
94
-    char *mDataDir;
95
-
96 96
     KeyboardButton keyBindings[ActionEventCount];
97 97
 };
98 98
 

+ 0
- 14
include/Render.h 查看文件

@@ -144,13 +144,6 @@ public:
144 144
     void addRoom(RenderRoom *rRoom);
145 145
 
146 146
     /*!
147
-     * \brief Starts and sets up OpenGL target
148
-     * \param width width of window
149
-     * \param height height of window
150
-     */
151
-    void Init(int width, int height);
152
-
153
-    /*!
154 147
      * \brief Loads textures in a certain id slot
155 148
      * \param image Image to load
156 149
      * \param width width of image
@@ -207,8 +200,6 @@ public:
207 200
 
208 201
     void setMode(int n);
209 202
 
210
-    void Update(int width, int height);
211
-
212 203
     /*!
213 204
      * \brief Renders a single game frame
214 205
      */
@@ -218,8 +209,6 @@ public:
218 209
 
219 210
     void ViewModel(entity_t *ent, int index);
220 211
 
221
-    void RegisterCamera(Camera *camera);
222
-
223 212
     void addSkeletalModel(SkeletalModel *mdl);
224 213
 
225 214
 private:
@@ -332,15 +321,12 @@ private:
332 321
     void tmpRenderModelMesh(model_mesh_t *r_mesh, texture_tri_t *ttri);
333 322
 
334 323
     Texture mTexture;                     //!< Texture subsystem
335
-    Camera *mCamera;                      //!< Camera subsystem
336 324
 
337 325
     std::vector<RenderRoom *> mRoomRenderList;
338 326
     std::vector<RenderRoom *> mRooms;
339 327
     std::vector<SkeletalModel *> mModels;
340 328
 
341 329
     unsigned int mFlags;                  //!< Rendering flags
342
-    unsigned int mWidth;                  //!< Viewport width
343
-    unsigned int mHeight;                 //!< Viewport height
344 330
     unsigned int mMode;                   //!< Rendering mode
345 331
     unsigned int *mNumTexturesLoaded;
346 332
     unsigned int *mNextTextureId;

+ 55
- 7
src/Game.cpp 查看文件

@@ -18,6 +18,39 @@
18 18
 #include "Game.h"
19 19
 #include "utils/strings.h"
20 20
 
21
+Game::Game() {
22
+    mLoaded = false;
23
+    mName = NULL;
24
+    mLara = NULL;
25
+
26
+    mTextureStart = 0;
27
+    mTextureLevelOffset = 0;
28
+    mTextureOffset = 0;
29
+
30
+    mCamera = NULL;
31
+    mRender = NULL;
32
+}
33
+
34
+Game::~Game() {
35
+    destroy();
36
+
37
+    if (mRender)
38
+        delete mRender;
39
+
40
+    if (mCamera)
41
+        delete mCamera;
42
+}
43
+
44
+int Game::initialize() {
45
+    mCamera = new Camera();
46
+    // TODO setup Camera
47
+
48
+    mRender = new Render();
49
+    mRender->initTextures(gOpenRaider->mDataDir, &mTextureStart, &mTextureLevelOffset);
50
+
51
+    return 0;
52
+}
53
+
21 54
 void percentCallbackTrampoline(int percent, void *data) {
22 55
     Game *self = static_cast<Game *>(data);
23 56
     self->percentCallback(percent);
@@ -27,9 +60,8 @@ void Game::percentCallback(int percent) {
27 60
 
28 61
 }
29 62
 
30
-// Throw exception with negative integer error code if fails
31
-Game::Game(const char *level) {
32
-    mLara = NULL;
63
+int Game::loadLevel(const char *level) {
64
+    destroy();
33 65
 
34 66
     mName = bufferString("%s", level);
35 67
 
@@ -37,7 +69,7 @@ Game::Game(const char *level) {
37 69
     gOpenRaider->mConsole->print("Loading %s", mName);
38 70
     int error = mTombRaider.Load(mName, percentCallbackTrampoline, this);
39 71
     if (error != 0) {
40
-        throw error;
72
+        return error;
41 73
     }
42 74
 
43 75
     // If required, load the external sound effect file MAIN.SFX into TombRaider
@@ -58,23 +90,39 @@ Game::Game(const char *level) {
58 90
         }
59 91
         delete [] tmp;
60 92
     }
93
+
94
+    mLoaded = true;
95
+    //mRender->setMode(Render::modeTexture);
96
+    // TODO enable Renderer
97
+    return 0;
61 98
 }
62 99
 
63
-Game::~Game() {
100
+void Game::destroy() {
64 101
     if (mName)
65 102
         delete [] mName;
103
+
104
+    mLoaded = false;
105
+    mRender->setMode(Render::modeDisabled);
66 106
 }
67 107
 
68 108
 void Game::handleAction(ActionEvents action, bool isFinished) {
109
+    if (mLoaded) {
69 110
 
111
+    }
70 112
 }
71 113
 
72 114
 void Game::handleMouseMotion(int xrel, int yrel) {
115
+    if (mLoaded) {
73 116
 
117
+    }
74 118
 }
75 119
 
76 120
 void Game::display() {
77
-    glClearColor(0.00f, 0.70f, 0.00f, 1.0f);
78
-    glClear(GL_COLOR_BUFFER_BIT);
121
+    if (mLoaded) {
122
+        //glClearColor(0.00f, 0.70f, 0.00f, 1.0f);
123
+        //glClear(GL_COLOR_BUFFER_BIT);
124
+
125
+        mRender->Display();
126
+    }
79 127
 }
80 128
 

+ 12
- 19
src/OpenRaider.cpp 查看文件

@@ -39,7 +39,7 @@ OpenRaider::OpenRaider() {
39 39
     mConsole = new Console();
40 40
     mSound = new Sound();
41 41
     mWindow = new WindowSDL();
42
-    mGame = NULL;
42
+    mGame = new Game();
43 43
 
44 44
     for (int i = 0; i < ActionEventCount; i++)
45 45
         keyBindings[i] = unknown;
@@ -179,17 +179,10 @@ int OpenRaider::command(const char *command, std::vector<char *> *args) {
179 179
             return -4;
180 180
         }
181 181
     } else if (strcmp(command, "load") == 0) {
182
-        if (mGame)
183
-            delete mGame;
184 182
         char *tmp = bufferString("%s/%s", mPakDir, args->at(0));
185
-        try {
186
-            mGame = new Game(tmp);
187
-            delete [] tmp;
188
-            return 0;
189
-        } catch (int error) {
190
-            delete [] tmp;
191
-            return error;
192
-        }
183
+        int error = mGame->loadLevel(tmp);
184
+        delete [] tmp;
185
+        return error;
193 186
     } else {
194 187
         mConsole->print("Unknown command: %s ", command);
195 188
         return -1;
@@ -605,6 +598,10 @@ int OpenRaider::initialize() {
605 598
     if (mSound->initialize() != 0)
606 599
         return -4;
607 600
 
601
+    // Initialize game engine
602
+    if (mGame->initialize() != 0)
603
+        return -5;
604
+
608 605
     mMenu->setVisible(true);
609 606
 
610 607
     mInit = true;
@@ -628,8 +625,7 @@ void OpenRaider::run() {
628 625
         glClear(GL_COLOR_BUFFER_BIT);
629 626
 
630 627
         // Draw game scene
631
-        if (mGame)
632
-            mGame->display();
628
+        mGame->display();
633 629
 
634 630
         // Draw 2D overlays (console and menu)
635 631
         mWindow->glEnter2D();
@@ -662,8 +658,7 @@ void OpenRaider::handleKeyboard(KeyboardButton key, bool pressed) {
662 658
         } else if (!mConsole->isVisible()) {
663 659
             for (int i = forwardAction; i < ActionEventCount; i++) {
664 660
                 if (keyBindings[i] == key) {
665
-                    if (mGame)
666
-                        mGame->handleAction((ActionEvents)i, pressed);
661
+                    mGame->handleAction((ActionEvents)i, pressed);
667 662
                 }
668 663
             }
669 664
         } else {
@@ -686,8 +681,7 @@ void OpenRaider::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton
686 681
     } else if (!mConsole->isVisible()) {
687 682
         for (int i = forwardAction; i < ActionEventCount; i++) {
688 683
             if (keyBindings[i] == button) {
689
-                if (mGame)
690
-                    mGame->handleAction((ActionEvents)i, !released);
684
+                mGame->handleAction((ActionEvents)i, !released);
691 685
             }
692 686
         }
693 687
     }
@@ -695,8 +689,7 @@ void OpenRaider::handleMouseClick(unsigned int x, unsigned int y, KeyboardButton
695 689
 
696 690
 void OpenRaider::handleMouseMotion(int xrel, int yrel) {
697 691
     if ((!mConsole->isVisible()) && (!mMenu->isVisible())) {
698
-        if (mGame)
699
-            mGame->handleMouseMotion(xrel, yrel);
692
+        mGame->handleMouseMotion(xrel, yrel);
700 693
     }
701 694
 }
702 695
 

+ 12
- 142
src/Render.cpp 查看文件

@@ -127,7 +127,6 @@ Render::Render()
127 127
 #ifdef USING_EMITTER
128 128
     mEmitter = 0x0;
129 129
 #endif
130
-    mCamera = 0x0;
131 130
     mSkyMesh = -1;
132 131
     mSkyMeshRotation = false;
133 132
     mMode = Render::modeDisabled;
@@ -138,9 +137,6 @@ Render::Render()
138 137
 
139 138
     mNextTextureId = NULL;
140 139
     mNumTexturesLoaded = NULL;
141
-
142
-    mWidth = 640;
143
-    mHeight = 480;
144 140
 }
145 141
 
146 142
 
@@ -156,7 +152,7 @@ Render::~Render()
156 152
 
157 153
 void Render::screenShot(char *filenameBase)
158 154
 {
159
-    mTexture.glScreenShot(filenameBase, mWidth, mHeight);
155
+    mTexture.glScreenShot(filenameBase, gOpenRaider->mWindow->mWidth, gOpenRaider->mWindow->mHeight);
160 156
 }
161 157
 
162 158
 
@@ -213,7 +209,7 @@ void Render::initTextures(char *textureDir, unsigned int *numLoaded,
213 209
         ++numTextures;
214 210
     }
215 211
 
216
-    snprintf(filename, 126, "%s%s", textureDir, "splash.tga");
212
+    snprintf(filename, 126, "%s/%s", textureDir, "splash.tga");
217 213
     filename[127] = 0;
218 214
 
219 215
     if ((bg_id = mTexture.loadTGA(filename)) > -1)
@@ -221,7 +217,7 @@ void Render::initTextures(char *textureDir, unsigned int *numLoaded,
221 217
         ++numTextures;
222 218
     }
223 219
 
224
-    snprintf(filename, 126, "%s%s", textureDir, "snow.tga");
220
+    snprintf(filename, 126, "%s/%s", textureDir, "snow.tga");
225 221
     filename[127] = 0;
226 222
 
227 223
     if ((snow1_id = mTexture.loadTGA(filename)) > -1)
@@ -229,7 +225,7 @@ void Render::initTextures(char *textureDir, unsigned int *numLoaded,
229 225
         ++numTextures;
230 226
     }
231 227
 
232
-    snprintf(filename, 126, "%s%s", textureDir, "snow2.tga");
228
+    snprintf(filename, 126, "%s/%s", textureDir, "snow2.tga");
233 229
     filename[127] = 0;
234 230
 
235 231
     if ((snow2_id = mTexture.loadTGA(filename)) > -1)
@@ -348,109 +344,6 @@ void renderTrace(int color, vec3_t start, vec3_t end)
348 344
 }
349 345
 
350 346
 
351
-void Render::Init(int width, int height)
352
-{
353
-    char *s;
354
-
355
-    mWidth = width;
356
-    mHeight = height;
357
-
358
-    // Print driver support information
359
-    printf("GL Vendor   : %s\n", glGetString(GL_VENDOR));
360
-    printf("GL Renderer : %s\n", glGetString(GL_RENDERER));
361
-    printf("GL Version  : %s\n", glGetString(GL_VERSION));
362
-    //printf("Extensions : %s\n\n\n", (char*)glGetString(GL_EXTENSIONS));
363
-
364
-    // Testing for goodies
365
-    // Mongoose 2001.12.31, Fixed string use to check for bad strings
366
-    s = (char*)glGetString(GL_EXTENSIONS);
367
-
368
-    if (s && s[0])
369
-    {
370
-        //printf("\tGL_ARB_multitexture       \t\t");
371
-
372
-        if (strstr(s, "GL_ARB_multitexture"))
373
-        {
374
-            mFlags |= Render::fMultiTexture;
375
-            //printf("YES\n");
376
-        }
377
-        /*
378
-        else
379
-        {
380
-            printf("NO\n");
381
-        }
382
-
383
-        printf("\tGL_EXT_texture_env_combine\t\t");
384
-
385
-        if (strstr(s, "GL_EXT_texture_env_combine"))
386
-        {
387
-            printf("YES\n");
388
-        }
389
-        else
390
-        {
391
-            printf("NO\n");
392
-        }
393
-        */
394
-    }
395
-
396
-    // Set up Z buffer
397
-    glEnable(GL_DEPTH_TEST);
398
-    glDepthFunc(GL_LESS);
399
-
400
-    // Set up culling
401
-    glEnable(GL_CULL_FACE);
402
-    glFrontFace(GL_CW);
403
-    //glFrontFace(GL_CCW);
404
-    //glCullFace(GL_FRONT);
405
-
406
-    // Set background to black
407
-    glClearColor(BLACK[0], BLACK[1], BLACK[2], BLACK[3]);
408
-
409
-    // Disable lighting
410
-    glDisable(GL_LIGHTING);
411
-
412
-    // Set up alpha blending
413
-    glEnable(GL_BLEND);
414
-    glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
415
-
416
-    //glEnable(GL_ALPHA_TEST); // Disable per pixel alpha blending
417
-    glAlphaFunc(GL_GREATER, 0);
418
-
419
-    glPointSize(5.0);
420
-
421
-    // Setup shading
422
-    glShadeModel(GL_SMOOTH);
423
-
424
-    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
425
-    glHint(GL_FOG_HINT, GL_NICEST);
426
-    glEnable(GL_COLOR_MATERIAL);
427
-    glEnable(GL_DITHER);
428
-
429
-    // AA polygon edges
430
-    glEnable(GL_POLYGON_SMOOTH);
431
-    glHint(GL_POLYGON_SMOOTH_HINT, GL_NICEST);
432
-
433
-    glDisable(GL_LINE_SMOOTH);
434
-    glDisable(GL_POINT_SMOOTH);
435
-    glDisable(GL_AUTO_NORMAL);
436
-    glDisable(GL_LOGIC_OP);
437
-    glDisable(GL_TEXTURE_1D);
438
-    glDisable(GL_STENCIL_TEST);
439
-    glDisable(GL_FOG);
440
-
441
-    glDisable(GL_NORMALIZE);
442
-
443
-    glEnableClientState(GL_VERTEX_ARRAY);
444
-    glDisableClientState(GL_EDGE_FLAG_ARRAY);
445
-    glDisableClientState(GL_COLOR_ARRAY);
446
-    glDisableClientState(GL_NORMAL_ARRAY);
447
-
448
-    glPolygonMode(GL_FRONT, GL_FILL);
449
-
450
-    glMatrixMode(GL_MODELVIEW);
451
-}
452
-
453
-
454 347
 void setLighting(bool on)
455 348
 {
456 349
     if (on)
@@ -548,13 +441,6 @@ void Render::setFlags(unsigned int flags)
548 441
 }
549 442
 
550 443
 
551
-void Render::Update(int width, int height)
552
-{
553
-    mWidth = width;
554
-    mHeight = height;
555
-}
556
-
557
-
558 444
 int Render::getMode()
559 445
 {
560 446
     return mMode;
@@ -638,13 +524,6 @@ void Render::Display()
638 524
     gl_test_reset();
639 525
 #endif
640 526
 
641
-    // Assertion: Rendering is disabled without texture or camera
642
-    if (!mCamera)
643
-    {
644
-        fprintf(stderr, "Render::Display> ERROR: No camera is registered\n");
645
-        return;
646
-    }
647
-
648 527
     switch (mMode)
649 528
     {
650 529
         case Render::modeDisabled:
@@ -705,16 +584,16 @@ void Render::Display()
705 584
             camPos[2] = curPos[2] + (64.0f * cosf(yaw));
706 585
         }
707 586
 
708
-        mCamera->setPosition(camPos);
587
+        gOpenRaider->mGame->mCamera->setPosition(camPos);
709 588
     }
710 589
 
711 590
     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
712 591
     glLoadIdentity();
713 592
 
714 593
     // Setup view in OpenGL with camera
715
-    mCamera->update();
716
-    mCamera->getPosition(camPos);
717
-    mCamera->getTarget(atPos);
594
+    gOpenRaider->mGame->mCamera->update();
595
+    gOpenRaider->mGame->mCamera->getPosition(camPos);
596
+    gOpenRaider->mGame->mCamera->getTarget(atPos);
718 597
     // Mongoose 2002.08.13, Quick fix to render OpenRaider upside down
719 598
     // gluLookAt(camPos[0], camPos[1], camPos[2], atPos[0], atPos[1], atPos[2], 0.0, -1.0, 0.0);
720 599
 
@@ -1107,13 +986,13 @@ void Render::drawObjects()
1107 986
         glPushMatrix();
1108 987
 
1109 988
 #ifdef USING_FPS_CAMERA
1110
-        mCamera->getPosition(curPos);
989
+        gOpenRaider->mGame->mCamera->getPosition(curPos);
1111 990
         glTranslated(curPos[0], curPos[1], curPos[2]);
1112
-        glRotated(mCamera->getYaw(), 0, 1, 0);
991
+        glRotated(gOpenRaider->mGame->mCamera->getYaw(), 0, 1, 0);
1113 992
         glTranslated(0, 500, 1200);
1114 993
 #else
1115 994
         glTranslated(LARA->pos[0], LARA->pos[1], LARA->pos[2]);
1116
-        glRotated(mCamera->getYaw(), 0, 1, 0);
995
+        glRotated(gOpenRaider->mGame->mCamera->getYaw(), 0, 1, 0);
1117 996
 #endif
1118 997
 
1119 998
         drawModel(static_cast<SkeletalModel *>(LARA->tmpHook));
@@ -1638,7 +1517,7 @@ void Render::drawSprite(sprite_t *sprite)
1638 1517
     glTranslated(sprite->pos[0], sprite->pos[1], sprite->pos[2]);
1639 1518
 
1640 1519
     // Sprites must always face camera, because they have no depth  =)
1641
-    glRotated(mCamera->getYaw(), 0, 1, 0);
1520
+    glRotated(gOpenRaider->mGame->mCamera->getYaw(), 0, 1, 0);
1642 1521
 
1643 1522
     switch (mMode)
1644 1523
     {
@@ -1956,15 +1835,6 @@ void Render::ViewModel(entity_t *ent, int index)
1956 1835
 }
1957 1836
 
1958 1837
 
1959
-void Render::RegisterCamera(Camera *camera)
1960
-{
1961
-    if (camera)
1962
-    {
1963
-        mCamera = camera;
1964
-    }
1965
-}
1966
-
1967
-
1968 1838
 void Render::addSkeletalModel(SkeletalModel *mdl)
1969 1839
 {
1970 1840
     mModels.push_back(mdl);

+ 3
- 3
src/Window.cpp 查看文件

@@ -98,9 +98,9 @@ int Window::initializeGL() {
98 98
 }
99 99
 
100 100
 void Window::resizeGL() {
101
-    float fovY = 45.0f;
102
-    float clipNear = 4.0f;
103
-    float clipFar = 4000.0f;
101
+    float fovY = 40.0f; // 45.0f
102
+    float clipNear = 10.0f; // 4.0f
103
+    float clipFar = 600000.0f; // 4000.0f
104 104
 
105 105
     glViewport(0, 0, mWidth, mHeight);
106 106
     glMatrixMode(GL_PROJECTION);

正在加载...
取消
保存