tavli

diff src/board.cc @ 22:c2a2069a49ec

slot highlighting, TODO blur
author John Tsiombikas <nuclear@member.fsf.org>
date Tue, 07 Jul 2015 21:56:37 +0300
parents c3fbf9616dbd
children 3e6430028d54
line diff
     1.1 --- a/src/board.cc	Thu Jul 02 00:01:39 2015 +0300
     1.2 +++ b/src/board.cc	Tue Jul 07 21:56:37 2015 +0300
     1.3 @@ -85,6 +85,7 @@
     1.4  Board::Board()
     1.5  {
     1.6  	piece_obj = 0;
     1.7 +	slot_sel = -1;
     1.8  	clear();
     1.9  
    1.10  	for(int i=0; i<NUM_SLOTS; i++) {
    1.11 @@ -208,6 +209,16 @@
    1.12  	return -1;
    1.13  }
    1.14  
    1.15 +void Board::select_slot(int idx)
    1.16 +{
    1.17 +	slot_sel = idx < 0 || idx >= NUM_SLOTS ? -1 : idx;
    1.18 +}
    1.19 +
    1.20 +int Board::get_selected_slot() const
    1.21 +{
    1.22 +	return slot_sel;
    1.23 +}
    1.24 +
    1.25  void Board::draw() const
    1.26  {
    1.27  	bool use_shadows = opt.shadows && sdr_shadow;
    1.28 @@ -232,38 +243,50 @@
    1.29  		piece_obj->draw();
    1.30  	}
    1.31  
    1.32 -	// draw the slot bounds
    1.33 -	/*
    1.34 -	static const float pal[][3] = {
    1.35 +	/*static const float pal[][3] = {
    1.36  		{1, 0, 0},
    1.37  		{0, 1, 0},
    1.38  		{0, 0, 1},
    1.39  		{1, 1, 0},
    1.40  		{0, 1, 1},
    1.41  		{1, 0, 1}
    1.42 -	};
    1.43 -	int idx = dbg_int % NUM_SLOTS;
    1.44 +	};*/
    1.45 +	int idx = slot_sel % NUM_SLOTS;
    1.46  	if(idx >= 0) {
    1.47  		glUseProgram(0);
    1.48  
    1.49  		glPushAttrib(GL_ENABLE_BIT);
    1.50  		glDisable(GL_LIGHTING);
    1.51  		glDisable(GL_CULL_FACE);
    1.52 -		glDisable(GL_DEPTH_TEST);
    1.53 +		glEnable(GL_DEPTH_TEST);
    1.54 +		glEnable(GL_TEXTURE_2D);
    1.55 +		glBindTexture(GL_TEXTURE_2D, img_highlight.texture());
    1.56 +		glEnable(GL_BLEND);
    1.57 +		glBlendFunc(GL_SRC_ALPHA, GL_ONE);
    1.58 +
    1.59 +		glDepthMask(0);
    1.60  
    1.61  		glBegin(GL_TRIANGLES);
    1.62 -		glColor3fv(pal[idx % (sizeof pal / sizeof *pal)]);
    1.63 +		//glColor3fv(pal[idx % (sizeof pal / sizeof *pal)]);
    1.64 +		glColor4f(1, 1, 1, 0.4);
    1.65 +		glTexCoord2f(0, 0);
    1.66  		glVertex3f(slotbb[idx].tri0.v[0].x, slotbb[idx].tri0.v[0].y, slotbb[idx].tri0.v[0].z);
    1.67 +		glTexCoord2f(1, 0);
    1.68  		glVertex3f(slotbb[idx].tri0.v[1].x, slotbb[idx].tri0.v[1].y, slotbb[idx].tri0.v[1].z);
    1.69 +		glTexCoord2f(1, 1);
    1.70  		glVertex3f(slotbb[idx].tri0.v[2].x, slotbb[idx].tri0.v[2].y, slotbb[idx].tri0.v[2].z);
    1.71 +		glTexCoord2f(0, 0);
    1.72  		glVertex3f(slotbb[idx].tri1.v[0].x, slotbb[idx].tri1.v[0].y, slotbb[idx].tri1.v[0].z);
    1.73 +		glTexCoord2f(1, 1);
    1.74  		glVertex3f(slotbb[idx].tri1.v[1].x, slotbb[idx].tri1.v[1].y, slotbb[idx].tri1.v[1].z);
    1.75 +		glTexCoord2f(0, 1);
    1.76  		glVertex3f(slotbb[idx].tri1.v[2].x, slotbb[idx].tri1.v[2].y, slotbb[idx].tri1.v[2].z);
    1.77  		glEnd();
    1.78  
    1.79 +		glDepthMask(1);
    1.80 +
    1.81  		glPopAttrib();
    1.82  	}
    1.83 -	*/
    1.84  	// TODO slot highlighting
    1.85  }
    1.86  
    1.87 @@ -477,6 +500,21 @@
    1.88  	return sqrt(x * x + y * y) < rad;
    1.89  }
    1.90  
    1.91 +static bool field_pattern(float x, float y)
    1.92 +{
    1.93 +	y = y < 0.5 ? y * 2.0 : 2.0 - y * 2.0;
    1.94 +	bool inside = false;
    1.95 +
    1.96 +	inside |= (spike(x, y + 0.33333) && !spike(x, y + 0.4)) ||
    1.97 +		(spike(x, y + 0.5) && !spike(x, y + 0.68));
    1.98 +	inside |= (circle(x, y, 0.12) && !circle(x, y, 0.1)) || circle(x, y, 0.06);
    1.99 +	inside |= (diamond(x, y) && !diamond(x, y - 0.015)) ||
   1.100 +		(diamond(x, y - 0.023) && !diamond(x, y - 0.028));
   1.101 +	inside |= center_circle(x, y, 0.03);
   1.102 +
   1.103 +	return inside;
   1.104 +}
   1.105 +
   1.106  bool Board::generate_textures()
   1.107  {
   1.108  	// ---- board field texture ----
   1.109 @@ -492,39 +530,49 @@
   1.110  		for(int j=0; j<img_field.width; j++) {
   1.111  			float u = (float)j / (float)img_field.width;
   1.112  
   1.113 -			int r = 0, g = 0, b = 0;
   1.114 +			// pattern mask
   1.115 +			bool inside = field_pattern(u, v);
   1.116  
   1.117  			float wood_val = wood(u, v);
   1.118 -
   1.119 -			// pattern mask
   1.120 -			float x = u;
   1.121 -			float y = v < 0.5 ? v * 2.0 : 2.0 - v * 2.0;
   1.122 -			bool inside = false;
   1.123 -
   1.124 -			inside |= (spike(x, y + 0.33333) && !spike(x, y + 0.4)) ||
   1.125 -				(spike(x, y + 0.5) && !spike(x, y + 0.68));
   1.126 -			inside |= (circle(x, y, 0.12) && !circle(x, y, 0.1)) || circle(x, y, 0.06);
   1.127 -			inside |= (diamond(x, y) && !diamond(x, y - 0.015)) ||
   1.128 -				(diamond(x, y - 0.023) && !diamond(x, y - 0.028));
   1.129 -			inside |= center_circle(x, y, 0.03);
   1.130 -
   1.131  			Vector3 wood_color = lerp(wcol1, wcol2, wood_val) * 0.9;
   1.132  			if(inside) {
   1.133  				wood_color = lerp(wcol1, wcol2, 1.0 - wood_val) * 2.0;
   1.134  			}
   1.135  
   1.136 -			r = (int)(wood_color.x * 255.0);
   1.137 -			g = (int)(wood_color.y * 255.0);
   1.138 -			b = (int)(wood_color.z * 255.0);
   1.139 +			int r = (int)(wood_color.x * 255.0);
   1.140 +			int g = (int)(wood_color.y * 255.0);
   1.141 +			int b = (int)(wood_color.z * 255.0);
   1.142  
   1.143  			pptr[0] = r > 255 ? 255 : r;
   1.144  			pptr[1] = g > 255 ? 255 : g;
   1.145  			pptr[2] = b > 255 ? 255 : b;
   1.146 -			pptr += 3;
   1.147 +			pptr[3] = 255;
   1.148 +			pptr += 4;
   1.149  		}
   1.150  	}
   1.151  	img_field.texture();
   1.152  
   1.153 +
   1.154 +	// ---- slot highlighting texture ----
   1.155 +	img_highlight.create(128, 256);
   1.156 +	pptr = img_highlight.pixels;
   1.157 +	for(int i=0; i<img_highlight.height; i++) {
   1.158 +		float v = (float)i / (float)img_highlight.height;
   1.159 +		for(int j=0; j<img_highlight.width; j++) {
   1.160 +			float u = (float)j / (float)img_highlight.width;
   1.161 +
   1.162 +			bool inside = field_pattern(u / 5.0, v * 0.445);
   1.163 +
   1.164 +			pptr[0] = 255;
   1.165 +			pptr[1] = 255;
   1.166 +			pptr[2] = 255;
   1.167 +			pptr[3] = inside ? 255 : 0;
   1.168 +			pptr += 4;
   1.169 +		}
   1.170 +	}
   1.171 +	img_highlight.texture();
   1.172 +
   1.173 +
   1.174  	// ---- generic wood texture ----
   1.175  	img_wood.create(256, 256);
   1.176  	pptr = img_wood.pixels;
   1.177 @@ -543,7 +591,8 @@
   1.178  			pptr[0] = r > 255 ? 255 : r;
   1.179  			pptr[1] = g > 255 ? 255 : g;
   1.180  			pptr[2] = b > 255 ? 255 : b;
   1.181 -			pptr += 3;
   1.182 +			pptr[3] = 255;
   1.183 +			pptr += 4;
   1.184  		}
   1.185  	}
   1.186  	img_wood.texture();
   1.187 @@ -576,8 +625,9 @@
   1.188  			pptr[0] = r > 255 ? 255 : (r < 0 ? 0 : r);
   1.189  			pptr[1] = g > 255 ? 255 : (g < 0 ? 0 : g);
   1.190  			pptr[2] = b > 255 ? 255 : (b < 0 ? 0 : b);
   1.191 +			pptr[3] = 255;
   1.192  
   1.193 -			pptr += 3;
   1.194 +			pptr += 4;
   1.195  		}
   1.196  	}
   1.197  	img_hinge.texture();