]> git.jsancho.org Git - c-irrlicht.git/blob - src/IVideoDriver.cpp
6bc1020f8cfab87bf61778c141f85a379bfe2e07
[c-irrlicht.git] / src / IVideoDriver.cpp
1 /* c-irrlicht --- C bindings for Irrlicht Engine
2
3    Copyright (C) 2019 Javier Sancho <jsf@jsancho.org>
4
5    This file is part of c-irrlicht.
6
7    c-irrlicht is free software; you can redistribute it and/or modify
8    it under the terms of the GNU Lesser General Public License as
9    published by the Free Software Foundation; either version 3 of the
10    License, or (at your option) any later version.
11
12    c-irrlicht is distributed in the hope that it will be useful, but
13    WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15    General Public License for more details.
16
17    You should have received a copy of the GNU Lesser General Public
18    License along with guile-irrlicht.  If not, see
19    <http://www.gnu.org/licenses/>.
20 */
21
22 #include <irrlicht/irrlicht.h>
23 #include <wchar.h>
24 #include "IVideoDriver.h"
25
26 extern "C" {
27   int
28   irr_video_beginScene(irr_video_IVideoDriver* driver,
29                        bool backBuffer,
30                        bool zBuffer,
31                        const irr_video_SColor* color,
32                        irr_video_SExposedVideoData* videoData,
33                        const irr_core_rect_s32* sourceRect)
34   {
35     // Color
36     irr::video::SColor col = irr::video::SColor(color->a, color->r,
37                                                 color->g, color->b);
38
39     // Video data
40     // TODO
41     irr::video::SExposedVideoData vdata = irr::video::SExposedVideoData();
42
43     // Source rect
44     irr::core::rect<irr::s32> rect;
45     if (sourceRect != NULL)
46       {
47         rect = irr::core::rect<irr::s32>(sourceRect->x,
48                                          sourceRect->y,
49                                          sourceRect->x2,
50                                          sourceRect->y2);
51       }
52
53     // Begin scene
54     return ((irr::video::IVideoDriver*)driver)
55       ->beginScene(backBuffer,
56                    zBuffer,
57                    col,
58                    vdata,
59                    sourceRect != NULL ? &rect : 0);
60   }
61
62   void
63   irr_video_drawVertexPrimitiveList(irr_video_IVideoDriver* driver,
64                                     const void* vertices,
65                                     uint32_t vertexCount,
66                                     const void* indexList,
67                                     uint32_t primCount,
68                                     irr_video_E_VERTEX_TYPE vType,
69                                     irr_scene_E_PRIMITIVE_TYPE pType,
70                                     irr_video_E_INDEX_TYPE iType)
71   {
72     // Convert vertices
73     irr::video::S3DVertex irrVertices[vertexCount];
74     for (int i=0; i<vertexCount; i++)
75       {
76         irr_video_S3DVertex *vertex = (irr_video_S3DVertex*)vertices + i;
77         irrVertices[i] =
78           irr::video::S3DVertex(vertex->pos.x, vertex->pos.y, vertex->pos.z,
79                                 vertex->normal.x, vertex->normal.y, vertex->normal.z,
80                                 irr::video::SColor(vertex->color.a,
81                                                    vertex->color.r,
82                                                    vertex->color.g,
83                                                    vertex->color.b),
84                                 vertex->tCoords.x, vertex->tCoords.y);
85       }
86
87     // Convert indices
88     size_t indexListSize =
89       ((iType == irr_video_EIT_16BIT) ? sizeof(irr::u16) : sizeof(irr::u32)) *
90       primCount * 3;
91     void* irrIndexList = malloc(indexListSize);
92
93     for (int i=0; i<primCount*3; i++)
94       {
95         if (iType == irr_video_EIT_16BIT)
96           {
97             uint16_t *index = (uint16_t*)indexList + i;
98             ((irr::u16*)irrIndexList)[i] = *index;
99           }
100         else
101           {
102             uint32_t *index = (uint32_t*)indexList + i;
103             ((irr::u32*)irrIndexList)[i] = *index;
104           }
105       }
106
107     ((irr::video::IVideoDriver*)driver)
108       ->drawVertexPrimitiveList(&irrVertices[0], vertexCount,
109                                 irrIndexList, primCount,
110                                 (irr::video::E_VERTEX_TYPE)vType,
111                                 (irr::scene::E_PRIMITIVE_TYPE)pType,
112                                 (irr::video::E_INDEX_TYPE)iType);
113
114     free(irrIndexList);
115   }
116
117   int
118   irr_video_endScene(irr_video_IVideoDriver* driver)
119   {
120     return ((irr::video::IVideoDriver*)driver)->endScene();
121   }
122
123   int
124   irr_video_getFPS(irr_video_IVideoDriver* driver)
125   {
126     return ((irr::video::IVideoDriver*)driver)->getFPS();
127   }
128
129   const char*
130   irr_video_getName(irr_video_IVideoDriver* driver)
131   {
132     const wchar_t *wname = ((irr::video::IVideoDriver*)driver)->getName();
133     size_t nbytes = wcslen(wname) + 1;
134     char *name = (char*)malloc(nbytes);
135     wcstombs(name, wname, nbytes);
136     return name;
137   }
138
139   irr_video_ITexture*
140   irr_video_getTexture(irr_video_IVideoDriver* driver,
141                        const char* filename)
142   {
143     return (irr_video_ITexture*)
144       ((irr::video::IVideoDriver*)driver)->getTexture(filename);
145   }
146
147   void
148   irr_video_setMaterial(irr_video_IVideoDriver* driver,
149                         irr_video_SMaterial* material)
150   {
151     ((irr::video::IVideoDriver*)driver)->setMaterial(*(irr::video::SMaterial*)material);
152   }
153
154   void
155   irr_video_setTransform(irr_video_IVideoDriver* driver,
156                          irr_video_E_TRANSFORMATION_STATE state,
157                          irr_core_matrix4* mat)
158   {
159     ((irr::video::IVideoDriver*)driver)
160       ->setTransform((irr::video::E_TRANSFORMATION_STATE)state,
161                      *(irr::core::matrix4*)mat);
162   }
163 }