Jump to content

  • Log In with Google      Sign In   
  • Create Account

Erik Rufelt

Member Since 17 Apr 2002
Online Last Active Today, 10:23 AM

Topics I've Started

std::thread arguments and copies

08 December 2015 - 07:13 PM

Encountered an interesting scenario, that I'm not sure if there's a guarantee for.

static void DestructionHandler(std::shared_ptr<Object> object) {
 while(!object.unique()) {
  sleep(100 ms);


void something() {
 std::shared_ptr<Object> object = std::make_shared<Object>();
 std::thread destructionHandler(DestructionHandler, object);

Is the loop guaranteed to exit, or could the underlying OS code that calls DestructionHandler have an extra reference to the object?

Is the loop guaranteed to exit, or could the underlying library code that calls DestructionHandler have an extra reference to the object (extra copy of the shared_ptr)?

CotBot City for Kids, on AppStore and Play

27 August 2014 - 04:14 AM

Hey everyone. Today is the release of a children's game I've been working on for about a year, together with my collaborators!

I've done all the coding, whereas others are in charge of the graphics (though a few of the simpler models are by me too, so I got to practice my Blender skills a bit =).


It's a driving game with mini-games depending on the vehicle you drive, mainly aimed at 3-6 year olds.


Check out CotBot City on AppStore or Google Play!


Attached File  splash_1280x720.jpg   114.1KB   12 downloads

Attached File  firetruck_1280x720.jpg   91.26KB   12 downloads

Attached File  driving_garbage_1280x720.jpg   108.07KB   12 downloads

Before I learned the ++ ..

22 October 2013 - 09:29 PM

Last modified January 2001, good old days.. ^^

// Global
struct Global
	// Windows stuff
	HINSTANCE					hInstance;
	HWND						hWnd;
	HDC							hDC;
	HGLRC						hRC;
	bool						active_app;
	bool						pressed_keys[256];
	bool						old_keys[256];
	// Direct Input
	//BYTE						di_buffer[256];
	// Screen info
	GLushort					screen_width;
	GLushort					screen_height;
	GLushort					bit_depth;
	// Loop done
	GLboolean					done;
	// Controls
	GLboolean					forward_pressed;
	GLboolean					backward_pressed;
	GLboolean					left_pressed;
	GLboolean					right_pressed;
	GLfloat						turn_angle;
	GLfloat						look_angle;
	GLboolean					new_left_button;
	GLboolean					new_right_button;
	GLboolean					left_button_pressed;
	GLboolean					left_button_was_pressed;
	GLboolean					right_button_pressed;
	GLboolean					right_button_was_pressed;
	GLboolean					shift_pressed;
	GLshort						mouse_x;
	GLshort						mouse_y;
	GLshort						mouse_x_start;
	GLshort						mouse_y_start;
	GLfloat						mouse_x_fraction;
	GLfloat						mouse_y_fraction;
	GLfloat						mouse_x_start_fraction;
	GLfloat						mouse_y_start_fraction;
	// FPS
	GLfloat						fps;
	// Global time (in milliseconds)
	unsigned long				time;
	// Trig tables
	GLfloat						cosTable[36000];
	GLfloat						sinTable[36000];
	// Main menu
	GLshort						main_menu_selection;
	GLshort						main_menu_which_menu;
	GLshort						main_menu_selection_red_change;
	GLshort						main_menu_selection_green_change;
	GLfloat						main_menu_selection_red;
	GLfloat						main_menu_selection_green;
	GLfloat						main_menu_x_move;
	GLfloat						main_menu_y_move;
	GLfloat						main_menu_points_x[11][11];
	GLfloat						main_menu_points_y[11][11];
	GLboolean					main_menu_up_pressed;
	GLboolean					main_menu_down_pressed;
	GLboolean					main_menu_confirm_pressed;
	GLfloat						angle;
	// Game state
	GLshort						game_state;
	GLboolean					end_game;
	// Camera
	GLfloat						camera_offset_x;
	GLfloat						camera_offset_y;
	GLfloat						camera_offset_z;
	GLfloat						camera_rot_x;
	GLfloat						camera_rot_y;
	GLfloat						camera_rot_z;
	// Textures
	GLuint						*textures;
	GLushort					texture_count;
	GLshort						at_texture;
	TextureFilePtr				texture_files;
	GLshort						current_texture;
	GLshort						added_textures;
	GLshort						num_global_textures;
	// Global textures
	GLshort						font_texture;
	GLshort						button_mark_texture;
	GLshort						selection_texture;
	GLshort						statusbar_texture;
	GLshort						buildbar_texture;
	GLshort						minimap_texture;
	GLshort						blood_texture;
	GLshort						gys_barracks_selection_texture;
	GLshort						gys_bc_selection_texture;
	GLshort						gys_utsatt_selection_texture;
	GLshort						cancel_button_texture;
	GLshort						grass_texture;
	GLshort						main_menu_texture;
	GLshort						single_texture;
	GLshort						host_texture;
	GLshort						join_texture;
	GLshort						options_texture;
	GLshort						quit_texture;
	// Sky dome
	GLshort						skydome_texture;
	GLshort						cloud_texture;
	GLshort						sky_box_start_texture;
	// Floor/wall textures
	GLshort						floor_texture;
	GLshort						wall_texture;
	// Players
	PlayerPtr					players;
	PlayerPtr					camera_player;
	// Structures/units
	UnitPtr						units;
	StructurePtr				structures;
	SelectionPtr				selection;
	char						*mouse_target;
	char						*highlighted_target;
	unsigned int				next_unit_id;
	unsigned int				next_structure_id;
	BloodSourcePtr				blood_sources;
	BloodPtr					blood;
	ProjectilePtr				projectiles;
	TerrainEffectPtr			terrain_effects;
	// Models
	SMPtr						model_gys_utsatt;
	SMPtr						model_gys_bc;
	SMPtr						model_gys_barracks;
	SMPtr						model_gys_arm;
	SMPtr						model_gys_leg;
	SMPtr						model_gys_torso;
	SMPtr						model_gys_head;
	// Resources
	GLshort						minerals;
	GLshort						gas;
	// Chat
	ChatMPtr					chat_messages;
	GLboolean					chatting;
	GLshort						chat_message_length;
	GLubyte						chat_message[256];
	// Button marks
	ButtonMarkPtr				button_marks;
	// Network
	Server						server;
	Client						client;
	GLshort						net_type;
	MessagePtr					messages;
	GLshort						ip_string_length;
	GLubyte						ip_string[256];
	// World
	GLfloat						world_y[101][101];
	GLfloat						world_shade[101][101];
	GLfloat						bottom_y;
	GLfloat						world_y_change[101][101];
	WorldTriangle				world_triangles[100][100][2];
	char						text[256];
	char						text2[256];
	GLboolean					do_it;
	FILE						*log_file;
	GLboolean					compressed;
typedef struct Global Global;

At least functions had representative names..

void Draw3DModelFast(SMPtr model, float offset_x, float offset_y, float offset_z,
					float rotation_x, float rotation_y, float rotation_z)
	BonePtr					bone = 0;
	int						i;
	int						j;
	float					x;
	float					y;
	float					z;
	float					shade;
		if(g.current_texture != (model->triList[i].texture+model->firstTexture))
			glBindTexture(GL_TEXTURE_2D, g.textures[model->triList[i].texture+model->firstTexture]);
			g.current_texture = model->triList[i].texture + model->firstTexture;
			x = model->vertexList[model->triList[i].vertex[j]].point.x;
			y = model->vertexList[model->triList[i].vertex[j]].point.y;
			z = model->vertexList[model->triList[i].vertex[j]].point.z;
			shade = model->vertexList[model->triList[i].vertex[j]].shade;
			glColor4f(shade, shade, shade, 1.0f);
			RotatePt(&x, &y, &z, rotation_x, rotation_y, rotation_z);
			x += offset_x;
			y += offset_y;
			z += offset_z;
			glVertex3f(x, y, z);
}// Draw3DModelFast

D3D11 Drawing Text

17 March 2011 - 07:46 PM

I've written a wrapper for drawing text in D3D11, using DirectWrite to draw the glyphs and format the text. DirectWrite text-layouts have a Draw method that enable callbacks for every part of the formatted text, which I use to get the text formatting and glyphs. Then every glyph is replaced with a quad that is drawn with D3D11, and every unique glyph is cached in a texture atlas. This works rather nicely, as DirectWrite is in charge of all formatting, text direction, unicode, glyph combining, word wrapping etc.

The question of fonts and D3D11 comes up rather often, so I hope it can benefit someone else too, and also I'd be happy if anyone has any suggestions for improvements, feature or code wise, or other comments. :) I wrote it using interfaces similar to D3D and DWrite to make it fit in nicely.

I uploaded it at CodePlex: http://fw1.codeplex.com/

Documentation for all the interfaces is available here: http://www.rufelt.com/fw1fontwrapper/

Simple one-file samples for using the library is also available at those locations.

Simple text drawing works like this:
#include "FW1FontWrapper.h"

void drawText(ID3D11DeviceContext *pContext) {
	IFW1Factory *pFW1Factory;
	HRESULT hResult = FW1CreateFactory(FW1_VERSION, &pFW1Factory);
	IFW1FontWrapper *pFontWrapper;
	hResult = pFW1Factory->CreateFontWrapper(pDevice, L"Arial", &pFontWrapper);
		L"Text",// String
		128.0f,// Font size
		100.0f,// X position
		50.0f,// Y position
		0xff0099ff,// Text color, 0xAaBbGgRr
		0// Flags

New screenshot:
Posted Image

Happy New Year!

31 December 2010 - 11:39 AM

Though depending on where you live it might be a few hours too early or too late. =)

Here was supposed to be a javascript fireworks but it seems it's not allowed :( A Google image will have to do. :)

[Edited by - Erik Rufelt on December 31, 2010 5:57:39 PM]