Problem converting c to cpp with matrices

Discuss the development of new homebrew software, tools and libraries.

Moderators: cheriff, TyRaNiD

Post Reply
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Problem converting c to cpp with matrices

Post by Ghoti »

Hi folks,

Hi folks,

i use the matrix functions that were part of the examples in the SDK:

Code: Select all

void matrix_multiply(float* result, float* a, float* b)
	{
		unsigned int i,j,k;
		float temp[16];

		for &#40;i = 0; i < 4; ++i&#41;
		&#123;
			for &#40;j = 0; j < 4; ++j&#41;
			&#123;
				float t = 0.0f;
				for &#40;k = 0; k < 4; ++k&#41;
					t += a&#91;&#40;k << 2&#41;+j&#93; * b&#91;&#40;i << 2&#41;+k&#93;;
				temp&#91;&#40;i << 2&#41;+j&#93; = t;
			&#125;
		&#125;

		memcpy&#40;result,temp,sizeof&#40;float&#41;*16&#41;;
	&#125;
when i use this in a C source file i use it like this:

Code: Select all

matrix_identity&#40;&#40;float*&#41;&world&#41;;


		
			matrix_identity&#40;&#40;float*&#41;&tmpworld&#41;;
			
			matrix_multiply&#40;&#40;float*&#41;&world, &world, &tmpworld&#41;;
this works fine.

since its part of a 3d game i want it to be in cpp cause of the object oriented way of programming.
well i copied the code but i got an error with that exact code:

Code: Select all

bj.cpp&#58;165&#58; error&#58; cannot convert 'ScePspFMatrix4*' to 'float*' for argument '2' to 'void matrix_multiply&#40;float*, float*, float*&#41;'
O
so i tried in doing (float*) infront of the &world, and &tmpworld. This stopped the error but my code is not running anymore.

my question: Why this difference in code? why does my old code won't work anymore?
rapso
Posts: 140
Joined: Mon Mar 28, 2005 6:35 am

Post by rapso »

how du u declare world and tmpworld? probably this are already pointers?
cooleyes
Posts: 123
Joined: Thu May 18, 2006 3:30 pm

Re: Problem converting c to cpp with matrices

Post by cooleyes »

Ghoti wrote:Hi folks,

Hi folks,

i use the matrix functions that were part of the examples in the SDK:

Code: Select all

void matrix_multiply&#40;float* result, float* a, float* b&#41;
	&#123;
		unsigned int i,j,k;
		float temp&#91;16&#93;;

		for &#40;i = 0; i < 4; ++i&#41;
		&#123;
			for &#40;j = 0; j < 4; ++j&#41;
			&#123;
				float t = 0.0f;
				for &#40;k = 0; k < 4; ++k&#41;
					t += a&#91;&#40;k << 2&#41;+j&#93; * b&#91;&#40;i << 2&#41;+k&#93;;
				temp&#91;&#40;i << 2&#41;+j&#93; = t;
			&#125;
		&#125;

		memcpy&#40;result,temp,sizeof&#40;float&#41;*16&#41;;
	&#125;
when i use this in a C source file i use it like this:

Code: Select all

matrix_identity&#40;&#40;float*&#41;&world&#41;;


		
			matrix_identity&#40;&#40;float*&#41;&tmpworld&#41;;
			
			matrix_multiply&#40;&#40;float*&#41;&world, &world, &tmpworld&#41;;
this works fine.

since its part of a 3d game i want it to be in cpp cause of the object oriented way of programming.
well i copied the code but i got an error with that exact code:

Code: Select all

bj.cpp&#58;165&#58; error&#58; cannot convert 'ScePspFMatrix4*' to 'float*' for argument '2' to 'void matrix_multiply&#40;float*, float*, float*&#41;'
O
so i tried in doing (float*) infront of the &world, and &tmpworld. This stopped the error but my code is not running anymore.

my question: Why this difference in code? why does my old code won't work anymore?
you must use force typecast
just like

Code: Select all

matrix_identity&#40;&#40;float*&#41;&tmpworld&#41;; 
matrix_multiply&#40;&#40;float*&#41;&world, &#40;float*&#41;&world, &#40;float*&#41;&tmpworld&#41;;
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

@rapso: I declare it in both situations this way:

Code: Select all

			ScePspFMatrix4	world;
			ScePspFMatrix4	tmpworld;
the only difference is that i this in cpp for in a class header and in the C source i do it just at the top of the source file

@cooleyes: yes i have done that, but why that difference, why do i have to do that in cpp and not in c?
chp
Posts: 313
Joined: Wed Jun 23, 2004 7:16 am

Post by chp »

Because C++ is not as relaxed when it comes to type checking as ordinary C is, and thats a good thing.

If you intend to always access ScePspFMatrix4-objects using that method, rewrite it to take a pointer of that type, or write a inlined overload that does the conversion for you.
GE Dominator
Ghoti
Posts: 288
Joined: Sat Dec 31, 2005 11:06 pm

Post by Ghoti »

ah okay thanks :)
Post Reply