#define _POSIX_C_SOURCE 200809L //for fileno and strdup
#include <stdio.h>
#include <fcntl.h>
#include <linux/fb.h>
#include <linux/input.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <assert.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include <time.h>
#include "config.h"
#include "common.h"
#include "graphics_c3.h"//not needed?
#include "graphics_fb.h"
#include "graphics_cs.h"
//typedef float real; //think this conflicts?
//TODO: will have to make some pixmaps get resized when the window does.
//for now set them to be as big as you think you'll ever resize the window to.
//#define SKYRULE 90
//#define SKYW (WIDTH*5)
//#define SKYH (HEIGHT/2)
//Pixmap skypixmap;
//char sky[SKYH][SKYW];
extern struct hvr_global global;
extern struct gra_global gra_global;
struct fb_global fb_global;
#ifdef GRAPHICAL
/*
struct plane {
c3_t p;
real xr;
real yr;
real zr;
}
void calculate_shape_color(c3_s_t s,real d) {
//given: at least 3 points on a plane.
//needed: angles of the plane.
//
xa=
ya=
za=
for(i=0;i<;i++) {
s.p.x
}
}
*/
void set_luminosity_color(int lum) {//need to come up with a good range for this.
fb_global.current_color=((200-lum) << 16) | ((200-lum) << 8) | lum;
}
void draw_cs_point(int x,int y) {//this should write to a backbuffer then I can memcopy it over.
unsigned int i=y * fb_global.info.xres + x;
if(x >= fb_global.info.xres) return;
if(y >= fb_global.info.yres) return;
if(i > fb_global.fblen) return;
//hack to test it with. remove me later.
//fb_global.current_color=-1;
int derp;
derp=(fb_global.current_color == -1) ? (rand()) : fb_global.current_color;
switch(fb_global.draw_mode) {
case DRAW_MODE_COPY:
fb_global.backbuf[i] = derp;
break;
case DRAW_MODE_OR:
fb_global.backbuf[i] |= derp;
break;
case DRAW_MODE_AND:
fb_global.backbuf[i] &= derp;
break;
case DRAW_MODE_CLEAR:
fb_global.backbuf[i]=0;
break;
default:
fprintf(stderr,"# derp. unknown draw_mode %d\n",fb_global.draw_mode);
break;
}
}
void draw_cs_arc(cs_t p1, cs_t p2) {//now... how to draw an arc.
// real r;//radius
// y=sin();
// x=cos();
}
int x_from_y(cs_t p1,cs_t p2,int y) {//get the value of x given a y within a line.
real m;
int b;
int x;
// y=mx+b MAGIC
if(p1.x == p2.x) return p1.x;//if this happens then we have a verticle line and can just shortcut this shit.
if(p1.y == p2.y) {//if this happens we have a horizontal line we're trying to find the X of based on a 'y' that probably isn't in the line...
return 0;
}//return p1.x;//WE SHOULD NOT GET HERE. fuck if I know.
// y=mx+b
// y-b=mx
// x=(y-b)/m
// b=y-mx
m=((real)(p1.y-p2.y))/((real)(p1.x-p2.x));
b=(int)((real)p1.y - (m * (real)p1.x));
x=(int)(((real)(y-b))/m);
if(!x) printf("x == %d y=%d m=%f b=%d\n",x,y,m,b);
return x;
}
//does this actually work? seems to.
void draw_cs_line(cs_t p1,cs_t p2) {//error somewhere in here. derp...
int x,y;
real m;
int b;
int xd,yd;
if(p1.x == p2.x) {
for(y=min(p1.y,p2.y);y<max(p1.y,p2.y);y++) {
draw_cs_point(p1.x,y);
}
return;
}
if(p1.y == p2.y) {
for(x=min(p1.x,p2.x);x<max(p1.x,p2.x);x++) {
draw_cs_point(x,p1.y);
}
return;
}
//this isn't working for some reason.
xd=p1.x<p2.x?1:-1;
yd=p1.y<p2.y?1:-1;
if(max(p1.x,p2.x)-min(p1.x,p2.x) > max(p1.y,p2.y)-min(p1.y,p2.y)) { //loop over x. like normal math. :P y=mx+b stuff.
m=((real)(p1.y-p2.y))/((real)(p1.x-p2.x));
b=(int)((real)p1.y - (m * (real)p1.x));
for(x=p1.x;x!=p2.x;x+=xd) {
y=(int)(m * (real)x + (real)b);
draw_cs_point(x,y);
}
} else { //loop over y
m=((real)(p1.x-p2.x))/((real)(p1.y-p2.y));
b=(int)((real)p1.x - (m * (real)p1.y));
for(y=p1.y;y!=p2.y;y+=yd) {
x=(int)(m * (real)y + (real)b);
draw_cs_point(x,y);
}
}
// ???
}
void draw_cs_text(cs_t p,char *text) {
/* char t[256];
int direction,ascent,descent;
XFontStruct *font=XLoadQueryFont(x11_global.dpy,"fixed");
XCharStruct overall;
snprintf(t,sizeof(t)-1,"%s",text);
XTextExtents(font,text,strlen(text),&direction,&ascent,&descent,&overall);
XDrawString(x11_global.dpy,x11_global.backbuffer,x11_global.backgc,p.x,p.y+(descent+ascent),text,strlen(text));*/
}
void draw_cs_shape(cs_s_t s) {//this is implemented as draw_cs_line... hrm. it could be moved up to graphics.c? probl no.
int i;//all cs shapes can have 1, 2, or 3+ points. guess I gotta do that logic here too.
switch(s.len) {
case 1:
//circle
//h=max(s.p[0].x,s.p[1].x)-min(s.p[0].x,s.p[1].x);
//XDrawArc(x11_global.dpy,x11_global.backbuffer,x11_global.backgc,s.p[0].x-h,s.p[0].y-h,h*2,h*2,0,360*64);
break;
case 2:
draw_cs_line(s.p[0],s.p[1]);
default:
for(i=0;i<s.len;i++) {
draw_cs_line(s.p[i],s.p[(i+1)%s.len]);
}
break;
}
}
void draw_cs_filled_shape(cs_s_t s) {//no circle handling atm. and only convex polygons.
int maxmax=0;
int minmin=0;
cs_t p1;
cs_t p2;
int i;
int y;
for(i=0