Fixed some little errors with the drawing functions.
[luagame.git] / funcs_draw.cpp
blobd7a3cb5feb7218d0c968e77d0cb9a47a11771aea
1 /*
2 Copyright (c)2006-2007 - Brett Lajzer
4 See LICENSE for license information.
5 */
7 #include <cmath>
8 #include <GL/gl.h>
9 #include <GL/glu.h>
10 #include <lua.hpp>
11 #include <SDL/SDL.h>
12 #include "globals.h"
13 #include "funcs_draw.h"
16 // direct drawing functions
19 //internal function for drawing circles and ellipses
20 void drawCircle(GLenum style, float x, float y, float rx, float ry, float r, float g, float b, float a, float rot, float scale_x, float scale_y){
21 glColor4f(r, g, b, a);
22 glDisable(GL_TEXTURE_RECTANGLE_ARB);
23 glPushMatrix();
24 glTranslatef(x, y, 0.0f);
25 glScalef(scale_x, scale_y, 1.0f);
26 glRotatef(rot, 0.0f, 0.0f, -1.0f);
28 glBegin(style);
29 for(double t = 0; t < 2*3.14159265358979; t += 0.05) {
30 glVertex2f(rx * cos(t), ry * sin(t));
32 glEnd();
34 glPopMatrix();
35 glColor3f(1.0f, 1.0f, 1.0f);
36 glEnable(GL_TEXTURE_RECTANGLE_ARB);
39 int l_draw_pixel(lua_State *L){
40 glColor4f(lua_tonumber(L,3)/255.0, lua_tonumber(L,4)/255.0, lua_tonumber(L,5)/255.0, lua_tonumber(L,6)/255.0);
41 glDisable(GL_TEXTURE_RECTANGLE_ARB);
43 glBegin(GL_POINTS);
44 glVertex2f((float)lua_tonumber(L,1), (float)lua_tonumber(L,2));
45 glEnd();
47 glColor3f(1.0f, 1.0f, 1.0f);
48 glEnable(GL_TEXTURE_RECTANGLE_ARB);
49 return 0;
52 int l_draw_line(lua_State *L){
53 float x1 = (float)lua_tonumber(L,1);
54 float y1 = (float)lua_tonumber(L,2);
55 float x2 = (float)lua_tonumber(L,3);
56 float y2 = (float)lua_tonumber(L,4);
58 glColor4f(lua_tonumber(L,5)/255.0, lua_tonumber(L,6)/255.0, lua_tonumber(L,7)/255.0, lua_tonumber(L,8)/255.0);
59 glDisable(GL_TEXTURE_RECTANGLE_ARB);
61 glBegin(GL_LINES);
62 glVertex2f(x1,y1);
63 glVertex2f(x2,y2);
64 glEnd();
66 glColor3f(1.0f, 1.0f, 1.0f);
67 glEnable(GL_TEXTURE_RECTANGLE_ARB);
68 return 0;
71 int l_draw_rect(lua_State *L){
72 float x1 = (float)lua_tonumber(L,1);
73 float y1 = (float)lua_tonumber(L,2);
74 float x2 = (float)lua_tonumber(L,3);
75 float y2 = (float)lua_tonumber(L,4);
76 float rot = (float)lua_tonumber(L,9);
77 float scale_x = (float)lua_tonumber(L,10);
78 float scale_y = (float)lua_tonumber(L,11);
80 glColor4f(lua_tonumber(L,5)/255.0, lua_tonumber(L,6)/255.0, lua_tonumber(L,7)/255.0, lua_tonumber(L,8)/255.0);
81 glDisable(GL_TEXTURE_RECTANGLE_ARB);
83 float w_half = (x2-x1)/2;
84 float h_half = (y2-y1)/2;
86 glPushMatrix();
87 glTranslatef(x1+w_half, y1+h_half, 0.0f);
89 glScalef(scale_x, scale_y, 1.0f);
90 glRotatef(rot, 0.0f, 0.0f, -1.0f);
92 glBegin(GL_LINE_LOOP);
93 glVertex2f(-w_half, -h_half);
94 glVertex2f(-w_half, h_half);
95 glVertex2f(w_half, h_half);
96 glVertex2f(w_half, -h_half);
97 glEnd();
98 glPopMatrix();
100 glColor3f(1.0f, 1.0f, 1.0f);
101 glEnable(GL_TEXTURE_RECTANGLE_ARB);
102 return 0;
105 int l_draw_frect(lua_State *L){
106 float x1 = (float)lua_tonumber(L,1);
107 float y1 = (float)lua_tonumber(L,2);
108 float x2 = (float)lua_tonumber(L,3);
109 float y2 = (float)lua_tonumber(L,4);
110 float rot = (float)lua_tonumber(L,9);
111 float scale_x = (float)lua_tonumber(L,10);
112 float scale_y = (float)lua_tonumber(L,11);
114 glColor4f(lua_tonumber(L,5)/255.0, lua_tonumber(L,6)/255.0, lua_tonumber(L,7)/255.0, lua_tonumber(L,8)/255.0);
115 glDisable(GL_TEXTURE_RECTANGLE_ARB);
117 float w_half = (x2-x1)/2;
118 float h_half = (y2-y1)/2;
120 glPushMatrix();
121 glTranslatef(x1+w_half, y1+h_half, 0.0f);
123 glScalef(scale_x, scale_y, 1.0f);
124 glRotatef(rot, 0.0f, 0.0f, -1.0f);
126 glBegin(GL_QUADS);
127 glVertex2f(-w_half, -h_half);
128 glVertex2f(-w_half, h_half);
129 glVertex2f(w_half, h_half);
130 glVertex2f(w_half, -h_half);
131 glEnd();
132 glPopMatrix();
134 glColor3f(1.0f, 1.0f, 1.0f);
135 glEnable(GL_TEXTURE_RECTANGLE_ARB);
136 return 0;
139 int l_draw_circle(lua_State *L){
140 drawCircle(GL_LINE_LOOP, (float)lua_tonumber(L,1), (float)lua_tonumber(L,2), (float)lua_tonumber(L,3), (float)lua_tonumber(L,3), lua_tonumber(L,4)/255.0, lua_tonumber(L,5)/255.0, lua_tonumber(L,6)/255.0, lua_tonumber(L,7)/255.0, (float)lua_tonumber(L,8), (float)lua_tonumber(L,9), (float)lua_tonumber(L,10));
141 return 0;
144 int l_draw_fcircle(lua_State *L){
145 drawCircle(GL_POLYGON, (float)lua_tonumber(L,1), (float)lua_tonumber(L,2), (float)lua_tonumber(L,3), (float)lua_tonumber(L,3), lua_tonumber(L,4)/255.0, lua_tonumber(L,5)/255.0, lua_tonumber(L,6)/255.0, lua_tonumber(L,7)/255.0, (float)lua_tonumber(L,8), (float)lua_tonumber(L,9)/255.0, (float)lua_tonumber(L,10));
146 return 0;
149 int l_draw_ellipse(lua_State *L){
150 drawCircle(GL_LINE_LOOP, (float)lua_tonumber(L,1), (float)lua_tonumber(L,2), (float)lua_tonumber(L,3), (float)lua_tonumber(L,4), lua_tonumber(L,5)/255.0, lua_tonumber(L,6)/255.0, lua_tonumber(L,7)/255.0, lua_tonumber(L,8)/255.0, (float)lua_tonumber(L,9),(float)lua_tonumber(L,10), (float)lua_tonumber(L,11));
151 return 0;
154 int l_draw_fellipse(lua_State *L){
155 drawCircle(GL_POLYGON, (float)lua_tonumber(L,1), (float)lua_tonumber(L,2), (float)lua_tonumber(L,3), (float)lua_tonumber(L,4), lua_tonumber(L,5)/255.0, lua_tonumber(L,6)/255.0, lua_tonumber(L,7)/255.0, lua_tonumber(L,8)/255.0, (float)lua_tonumber(L,9), (float)lua_tonumber(L,10), (float)lua_tonumber(L,11));
156 return 0;