Changeset 40
- Timestamp:
- 07/10/07 19:03:27 (1 year ago)
- Files:
-
- trunk/src/demo1/main.d (modified) (4 diffs)
- trunk/src/yage/gui/surface.d (modified) (6 diffs)
- trunk/src/yage/system/device.d (modified) (3 diffs)
- trunk/src/yage/system/input.d (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/demo1/main.d
r37 r40 59 59 60 60 Surface disp = new Surface(null); 61 disp.texture = camera.getTexture();62 61 disp.topLeft = Vec2f(0,0); 63 disp.bottomRight = Vec2f(1, 1);64 disp. map();62 disp.bottomRight = Vec2f(1, 1); 63 disp.setVisibility(true); 65 64 65 Surface first = new Surface(disp); 66 first.texture = camera.getTexture(); 67 first.topLeft = Vec2f(0,0); 68 first.bottomRight = Vec2f(.75, .75); 69 first.setVisibility(true); 66 70 71 Surface second = new Surface(disp); 72 second.texture = camera.getTexture(); 73 second.topLeft = Vec2f(.25,.25); 74 second.bottomRight = Vec2f(1,1); 75 second.setVisibility(true); 76 77 void onMousedown(Surface self, byte buttons, Vec2i coordinates){ 78 self.raise(); 79 Input.button[1].up = false; 80 Input.setGrabMouse(!Input.getGrabMouse()); 81 } 82 83 void onResize(Surface self, Vec2i difference){ 84 camera.setResolution(self.size.x, self.size.y); 85 } 86 87 first.onMousedown = &onMousedown; 88 second.onMousedown = &onMousedown; 89 first.onResize = &onResize; 67 90 68 91 // Music … … 102 125 103 126 // Toggle mouse grab 104 if (Input.button[1].up)105 { Input.button[1].up = false;106 Input.setGrabMouse(!Input.getGrabMouse());107 }127 // if (Input.button[1].up) 128 // { Input.button[1].up = false; 129 // Input.setGrabMouse(!Input.getGrabMouse()); 130 // } 108 131 ship.getSpring().update(1/60.0f); 109 132 } … … 111 134 112 135 Device.resizeWindow(800, 600); 113 disp.recalculateTexture();136 //disp.recalculateTexture(); 114 137 115 138 // Rendering / Input Loop … … 127 150 Input.processInput(); 128 151 camera.toTexture(); 129 Device.render(); 130 152 disp.render(); 153 154 131 155 // Print framerate 132 156 fps++; trunk/src/yage/gui/surface.d
r38 r40 5 5 import yage.system.device; 6 6 import derelict.opengl.gl; 7 7 import derelict.sdl.sdl; 8 import yage.gui.style; 8 9 import yage.system.constant; 9 10 … … 11 12 12 13 class Surface{ 13 14 15 GPUTexture texture; 16 17 Surface[] subs; 18 19 Vec2f portion; 14 //Style style; //move style into a higher level clas, perhaps make a geometry struct isntead 15 GPUTexture texture; //Change from GPUTexture to Texture or Material 16 17 //Linked list would be faster for raising a window. 18 Surface[] subs;//Perhaps this should be changed into a linked list... 19 //Not sure if I should have a reference to Parent or not, but for now, I will. 20 Surface parent; 21 22 Vec2f portion; //used for the texture only 20 23 21 24 Vec2f topLeft; 22 25 Vec2f bottomRight; 23 26 24 Vec2i position; 27 //these are used for rendering, not calculation 28 Vec2i position1; 25 29 Vec2i position2; 26 30 31 27 32 Vec2i size; 28 33 29 bool mapped;34 bool visible; 30 35 31 36 //add root position and stuff 32 37 33 void delegate() onResize; 34 35 this(Surface parent){ 38 //Not sure how to impelement gluUnProject 39 40 void delegate(typeof(this) self) onBlur; 41 void blur(){ if(onBlur)onBlur(this);} 42 43 void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onClick; 44 45 void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onDblclick; 46 47 void delegate(typeof(this) self) onFocus; 48 49 void delegate(typeof(this) self, byte key, byte modifiers) onKeydown; 50 51 void delegate(typeof(this) self, byte key, byte modifiers) onKeypress; 52 53 void delegate(typeof(this) self, byte key, byte modifiers) onKeyup; 54 55 void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onMousedown; 56 void mousedown(byte buttons, Vec2i coordinates){ if(onMousedown)onMousedown(this, buttons, coordinates);} 57 58 void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onMousemove; 59 void mousemove(byte buttons, Vec2i coordinates){ if(onMousemove)onMousemove(this, buttons, coordinates);} 60 61 void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onMouseout; 62 63 void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onMouseover; 64 65 void delegate(typeof(this) self, byte buttons, Vec2i coordinates) onMouseup; 66 void mouseup(byte buttons, Vec2i coordinates){ if(onMouseup)onMouseup(this, buttons, coordinates);} 67 68 void delegate(typeof(this) self, Vec2i difference) onResize; 69 70 71 this(Surface p){ 72 parent = p; 36 73 if(parent is null){ 37 74 Device.subs ~= this; 75 recalculate(Device.getHeight(), Device.getWidth()); 38 76 } 39 77 else{ 40 78 parent.subs ~= this; 79 recalculate(parent.size.x, parent.size.y); 41 80 } 42 81 } … … 48 87 49 88 void recalculate(int width, int height){ //not done 50 position .x = cast(int)(topLeft.x * cast(float)width);51 position .y = cast(int)(topLeft.y * cast(float)height);89 position1.x = cast(int)(topLeft.x * cast(float)width); 90 position1.y = cast(int)(topLeft.y * cast(float)height); 52 91 53 92 position2.x = cast(int)(bottomRight.x * cast(float)width); 54 93 position2.y = cast(int)(bottomRight.y * cast(float)height); 55 94 56 size.x = position2.x - position.x; 57 size.y = position2.y - position.y; 58 59 foreach(sub ;this.subs) sub.recalculate(size.x, size.y); 95 Vec2i temp = size; 96 97 size.x = position2.x - position1.x; 98 size.y = position2.y - position1.y; 99 100 if(onResize)onResize(this, Vec2i(temp.x - size.x, temp.y - size.y)); 101 102 foreach(sub ;this.subs) 103 sub.recalculate(size.x, size.y); 104 } 105 106 void recalculate(){ 107 if(parent == null) 108 recalculate(Device.width, Device.height); 109 else 110 recalculate(parent.size.x, parent.size.y); 60 111 } 61 112 … … 65 116 } 66 117 118 void render(){ 119 glPushAttrib(0xFFFFFFFF); // all attribs 120 121 // Setup the viewport in orthogonal mode, 122 // with dimensions 0..width, 0..height 123 // with 0,0 being at the top left. 124 glViewport(0, 0, Device.width, Device.height); 125 glMatrixMode(GL_PROJECTION); 126 glLoadIdentity(); 127 glOrtho(0, Device.width, Device.height, 0, -1, 1); 128 glMatrixMode(GL_MODELVIEW); 129 glLoadIdentity(); 130 131 glDisable(GL_DEPTH_TEST); 132 glDisable(GL_LIGHTING); 133 134 glEnable(GL_TEXTURE_2D); 135 136 //This may need to be changed for when people wish to render surfaces individually so the already rendered are not cleared. 137 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 138 139 draw(); 140 141 SDL_GL_SwapBuffers(); 142 143 glPopAttrib(); 144 } 145 67 146 void draw(){ 68 recalculateTexture(); 69 if(mapped){ 147 if(visible){ 70 148 if (texture !is null){ 149 recalculateTexture(); 71 150 // Draw a textured quad of our current material 72 151 Texture(texture, true, TEXTURE_FILTER_BILINEAR).bind(); … … 75 154 76 155 glTexCoord2f(0, 0); 77 glVertex2 f(position.x, bottomRight.y);156 glVertex2i(position1.x, position2.y); 78 157 79 glTexCoord2f(portion.x, 0); 80 glVertex2 f(bottomRight.x, bottomRight.y);158 glTexCoord2f(portion.x, 0); 159 glVertex2i(position2.x, position2.y); 81 160 82 161 glTexCoord2f(portion.x, portion.y); 83 glVertex2 f(bottomRight.x, topLeft.y);162 glVertex2i(position2.x, position1.y); 84 163 85 164 glTexCoord2f(0, portion.y); 86 glVertex2 f(topLeft.x, topLeft.y);165 glVertex2i(position1.x, position1.y); 87 166 88 167 glEnd(); … … 92 171 } 93 172 94 void map(){ 95 mapped = true; 96 } 97 void unmap(){ 98 mapped = false; 99 } 100 } 173 void setVisibility(bool v){ 174 visible = v; 175 } 176 177 void raise(){ //Could be cleaner, whatever, I'll fix it later 178 if(parent is null){ 179 uint index = findIndex(this, Device.subs); 180 for(; index < Device.subs.length - 1; index++) 181 Device.subs[index] = Device.subs[index+1]; 182 Device.subs[$] = this; 183 } 184 else{ 185 uint index = findIndex(this, parent.subs); 186 for(; index < parent.subs.length - 1; index++) 187 parent.subs[index] = parent.subs[index+1]; 188 parent.subs[$-1] = this; 189 } 190 } 191 } 192 193 Surface findSurface(int x, int y){ 194 foreach_reverse(sub; Device.subs){ 195 if(sub.position1.x <= x && x <= sub.position2.x && sub.position1.y <= y && y <= sub.position2.y){ 196 return findSurface(sub, x, y); 197 } 198 } 199 return null; 200 } 201 202 Surface findSurface(Surface surface,int x, int y){ 203 foreach_reverse(sub; surface.subs){ 204 if(sub.position1.x <= x && x <= sub.position2.x && sub.position1.y <= y && y <= sub.position2.y){ 205 return findSurface(sub, x, y); 206 } 207 } 208 return surface; 209 } 210 211 uint findIndex(Surface surface, Surface[] array){ //perhaps put into a template 212 foreach(uint index, Surface current; array){ 213 if(current == surface) return index; 214 } 215 return 1 << 8; 216 } trunk/src/yage/system/device.d
r38 r40 27 27 // Enable specular highlights with textures. 28 28 const int LIGHT_MODEL_COLOR_CONTROL_EXT = 0x81F8; 29 const int SINGLE_COLOR_EXT = 0x81F9;29 const int SINGLE_COLOR_EXT = 0x81F9; 30 30 const int SEPARATE_SPECULAR_COLOR_EXT = 0x81FA; 31 31 … … 190 190 SDL_EnableUNICODE(true); 191 191 SDL_EnableKeyRepeat(1, 100); 192 Input.setGrabMouse(true);192 //Input.setGrabMouse(true); 193 193 194 194 // Initialize OpenAL … … 275 275 } 276 276 277 /// Draw the current material to the screen.278 static void render(){279 glPushAttrib(0xFFFFFFFF); // all attribs280 281 // Setup the viewport in orthogonal mode,282 // with dimensions 0..width, 0..height283 // with 0,0 being at the top left.284 glViewport(0, 0, width, height);285 glMatrixMode(GL_PROJECTION);286 glLoadIdentity();287 glOrtho(0, 1, 1, 0, -1, 1);288 glMatrixMode(GL_MODELVIEW);289 glLoadIdentity();290 291 glDisable(GL_DEPTH_TEST);292 glDisable(GL_LIGHTING);293 294 glEnable(GL_TEXTURE_2D);295 296 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);297 foreach(sub; this.subs)298 sub.draw();299 300 SDL_GL_SwapBuffers();301 302 //Texture(texture, true, TEXTURE_FILTER_BILINEAR).bind();303 glPopAttrib();304 }305 306 277 /// Return the aspect ratio (width/height) of the rendering window. 307 278 static float getAspectRatio() trunk/src/yage/system/input.d
r28 r40 12 12 import yage.core.vector; 13 13 import yage.system.device; 14 import yage.gui.surface; 14 15 15 16 … … 76 77 button[event.button.button].xdown = mousex; 77 78 button[event.button.button].ydown = mousey; 79 80 auto surface = findSurface(mousex, mousey); 81 if(!(surface is null)) surface.mousedown(event.button.button, Vec2i(mousex,mousey)); 82 78 83 break; 79 84 case SDL_MOUSEBUTTONUP: … … 82 87 button[event.button.button].xup = mousex; 83 88 button[event.button.button].yup = mousey; 89 90 auto surface = findSurface(mousex, mousey); 91 if(!(surface is null)) surface.mouseup(event.button.button, Vec2i(mousex,mousey)); 92 84 93 break; 85 94 case SDL_MOUSEMOTION: … … 92 101 mousex = event.motion.x; 93 102 mousey = event.motion.y; 103 104 //auto surface = findSurface(mousex, mousey); 105 //if(!(surface is null)) surface.mousemove(event.button.button, Vec2i(mousex,mousey)); 94 106 break; 95 107
