View difference between Paste ID: 6C2RTMb2 and R7Daf8n5
SHOW: | | - or go back to the newest paste.
1-
#include "Draw.h"
1+
#ifndef DRAW_H_INCLUDED
2-
///vars to add to structs
2+
#define DRAW_H_INCLUDED
3
4-
	float bw,bh;//blit width/height if non pwr 2 textures
4+
#include <GL/gl.h>
5
#include <GL/glext.h>
6
#include <stdint.h>
7-
	float bw,bh;
7+
8
9
///OGL texture structs
10-
///Draw Funcs to replace (also replace "GL_TEXTURE_RECTANGLE_NV" with "GL_TEXTURE_2D")
10+
//textures are the format OGL blits, non editable data -(loaded sprites/images)
11-
bool IsPowerOfTwo(uint32_t x)
11+
12-
{
12+
	protected:
13-
    return ((x & (x - 1)) == 0);
13+
	bool texture_generated;
14-
}
14+
15-
int HighestBit(uint32_t x)
15+
	public:
16-
{
16+
	GLuint Texture_ID;
17-
	if (IsPowerOfTwo(x)){return x;}
17+
	uint32_t w,h;
18-
	uint32_t y = 1<<31;
18+
19-
	while (y>x){y=y>>1;}
19+
	void gen_texture(uint16_t w, uint16_t h, uint8_t *PixData);
20-
 	return y<<1;
20+
21-
}
21+
	OGL_Texture();
22-
void OGL_Texture::gen_texture(uint16_t width, uint16_t height, uint8_t *PixData){
22+
	~OGL_Texture();
23-
	if (width==0 || height==0){printf("ERROR! WIDTH OR HEIGHT OF TEXTURE == 0"); exit(1);}
23+
24-
	//delete old texture if existing
24+
//surfaces contain editable pixel data, can be cast to textures for blitting -(effects, debug/primitive data draws)
25-
	if (texture_generated && Texture_ID){
25+
26-
		glDeleteTextures(1,&Texture_ID);
26+
	uint32_t w,h;
27-
	}
27+
	uint8_t bpp;
28-
	//Power of two texture support
28+
	uint8_t *PixData;
29-
	w = width;
29+
30-
	h = height;
30+
	void Create_Surface(uint16_t w,uint16_t h, char bpp);
31-
	uint8_t *NewPixData = PixData;
31+
	void Clear_Surface(uint8_t r, uint8_t g, uint8_t b, uint8_t a=255);
32-
	bool todelete = false;
32+
	void Set_Surface_AlphaKey(uint8_t r, uint8_t g, uint8_t b);
33-
	if (!(IsPowerOfTwo(w) && IsPowerOfTwo(h))){
33+
	void gen_texture();
34-
		uint32_t new_w = HighestBit(w);
34+
35-
		uint32_t new_h = HighestBit(h);
35+
	OGL_Surface();
36-
		NewPixData = new uint8_t[new_w*new_h*4];
36+
	~OGL_Surface();
37-
		memset(NewPixData,255, new_w*new_h*4);
37+
38-
		uint32_t pos1 = 0;
38+
39-
		uint32_t pos2 = 0;
39+
OGL_Texture *Texture_Cast(OGL_Surface *Surface);
40-
		for (uint32_t y = 0; y<h; y++){
40+
41-
			for (uint32_t x = 0; x<w; x++){
41+
42-
				NewPixData[pos1]  = PixData[pos2];
42+
43-
				NewPixData[pos1+1]= PixData[pos2+1];
43+
44-
				NewPixData[pos1+2]= PixData[pos2+2];
44+
enum Image_Options{
45-
				NewPixData[pos1+3]= PixData[pos2+3];
45+
	IMG_NORM  = 0,
46-
				pos1+=4;
46+
	IMG_HFLIP = 1,
47-
				pos2+=4;
47+
	IMG_VFLIP = 2,
48-
			}
48+
	IMG_ADD_BLEND = 3
49-
			pos1+=(new_w-w)*4;
49+
50-
		}
50+
void DrawImage(OGL_Texture *Texture, int dx, int dy, float rot=0.0,float scale_x=1.0,float scale_y=1.0,float alpha=1.0,int options=0);
51-
		width= new_w;
51+
void DrawSubImage(OGL_Texture *Texture, int sx, int sy, int sw, int sh, int dx, int dy, float rot=0.0,float scale_x=1.0,float scale_y=1.0,float alpha=1.0,int options=0);
52-
		height= new_h;
52+
53-
		bw = float(w)/float(new_w);
53+
//////////
54-
		bh = float(h)/float(new_h);
54+
//COLOUR//
55-
		todelete = true;
55+
//////////
56-
	}
56+
uint32_t make_col(uint8_t r, uint8_t g, uint8_t b, uint8_t a);
57-
	//generate texture
57+
uint32_t make_col(uint8_t r, uint8_t g, uint8_t b);
58-
	glGenTextures(1,&Texture_ID);
58+
59-
	if (Texture_ID==0){printf("ERROR IN TEXTURE GENERATION! NO DEVICE CONTEXT?"); exit(1);}
59+
///////////////////
60-
	glEnable(GL_TEXTURE_2D);
60+
//DRAW PRIMITIVES//
61-
		glBindTexture(GL_TEXTURE_2D, Texture_ID);
61+
///////////////////
62-
		//2d texture, level of detail, number of components (3=rgb), w,h, border, byte order, data format, data
62+
bool putpixel(OGL_Surface *Surface, int x, int y, uint32_t col);
63-
		glTexImage2D(GL_TEXTURE_2D, 0,GL_RGBA8, width, height, 0, GL_BGRA,GL_UNSIGNED_BYTE, NewPixData);
63+
bool getpixel(OGL_Surface *Surface, int x, int y, uint32_t &col);
64-
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
64+
//
65-
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
65+
void hline(OGL_Surface *Surface, int x, int y, int w, uint32_t col);
66-
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
66+
void vline(OGL_Surface *Surface, int x, int y, int h, uint32_t col);
67-
		glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
67+
void line(OGL_Surface *Surface, int x1, int y1, int x2, int y2, uint32_t col);
68-
		glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
68+
void box(OGL_Surface *Surface, int x, int y, int w, int h, uint32_t col);
69-
		glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, GL_REPLACE);
69+
void rect(OGL_Surface *Surface, int x, int y, int w, int h, uint32_t col);
70-
	glDisable(GL_TEXTURE_2D);
70+
void polyline(OGL_Surface *Surface, unsigned char n, int points[], uint32_t col);
71-
	texture_generated = true;
71+
void polygon(OGL_Surface *Surface, unsigned char n, int points[], uint32_t col);
72-
	//cleanup
72+
void circle(OGL_Surface *Surface, int x0, int y0, int radius, uint32_t col);
73-
	if (todelete){delete [] NewPixData;}
73+
void circle_fill(OGL_Surface *Surface, int x0, int y0, int radius, uint32_t col);
74-
}
74+
//
75
76-
///////////////
76+
#endif // DRAW_H_INCLUDED