POV-Ray : Newsgroups : povray.off-topic : Learning C++ Server Time
6 Sep 2024 19:23:15 EDT (-0400)
  Learning C++ (Message 48 to 57 of 57)  
<<< Previous 10 Messages Goto Initial 10 Messages
From: Warp
Subject: Re: Learning C++
Date: 13 Dec 2008 10:30:56
Message: <4943d52f@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> Or do people just 
> interface to C libraries for that sort of thing?

  I think most libraries out there are written in C rather than C++.
I suppose the ideology is something like "if I write it in C, it will
be still usable in C++, but I will get a much larger group of users
this way".

  Sometimes someone will then write a C++ wrapper to make it easier to
use the library. The quality of such C++ wrappers vary wildly. (In my
little experience the average tends to be on the worse side, unfortunately.)

-- 
                                                          - Warp


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Learning C++
Date: 13 Dec 2008 10:55:40
Message: <4943dafc@news.povray.org>
Warp wrote:
> Darren New <dne### [at] sanrrcom> wrote:
>> Or do people just
>> interface to C libraries for that sort of thing?
> 
>   I think most libraries out there are written in C rather than C++.
> I suppose the ideology is something like "if I write it in C, it will
> be still usable in C++, but I will get a much larger group of users
> this way".
> 
>   Sometimes someone will then write a C++ wrapper to make it easier to
> use the library. The quality of such C++ wrappers vary wildly. (In my
> little experience the average tends to be on the worse side,
> unfortunately.)

A good approach (that few people use) is making the library in C++ and
making a C wrapper :)


Post a reply to this message

From: nemesis
Subject: Re: Learning C++
Date: 13 Dec 2008 11:06:56
Message: <4943dda0$1@news.povray.org>
Nicolas Alvarez wrote:
> No they aren't. X doesn't provide any UI widgets as far as I know.

Hmm...

nemesis@nemesis-desktop:~$ ls -lh /usr/lib/libXaw7.so.7.0.0
-rw-r--r-- 1 root root 358K 2007-07-24 07:00 /usr/lib/libXaw7.so.7.0.0

Looks still in place...


http://en.wikipedia.org/wiki/Xaw


Post a reply to this message

From: Warp
Subject: Re: Learning C++
Date: 13 Dec 2008 11:22:20
Message: <4943e13c@news.povray.org>
Nicolas Alvarez <nic### [at] gmailcom> wrote:
> A good approach (that few people use) is making the library in C++ and
> making a C wrapper :)

  That would be rather difficult because it's not possible to call C++
functions from C. (The C compiler cannot understand C++ name mangling,
and obviously none of the features C++ has that C doesn't.)

-- 
                                                          - Warp


Post a reply to this message

From: Nicolas Alvarez
Subject: Re: Learning C++
Date: 13 Dec 2008 11:39:17
Message: <4943e534@news.povray.org>
Warp wrote:
> Nicolas Alvarez <nic### [at] gmailcom> wrote:
>> A good approach (that few people use) is making the library in C++ and
>> making a C wrapper :)
> 
>   That would be rather difficult because it's not possible to call C++
> functions from C. (The C compiler cannot understand C++ name mangling,
> and obviously none of the features C++ has that C doesn't.)

Isn't this possible?

cwrap.h:

struct Foo_Obj; //fwd declaration

#ifdef __cplusplus
extern "C" {
#endif

struct Foo_Obj* foo_obj_create();
void foo_obj_dosomething(Foo_Obj* obj);
void foo_obj_delete(Foo_Obj* obj);

#ifdef __cplusplus
}
#endif

cwrap.cpp:

#include "foo.hpp"

struct Foo_Obj {
    FooLib::Obj realthing;
};

extern "C" {

struct Foo_Obj* foo_obj_create() {
    return new Foo_Obj;
}
void foo_obj_delete(struct Foo_Obj* obj) {
    delete obj;
}
void foo_obj_dosomething(struct Foo_Obj* obj) {
    obj->realthing.dosomething();
}

} //extern "C"


Post a reply to this message

From: triple r
Subject: Re: Learning C++
Date: 17 Dec 2008 23:55:00
Message: <web.4949d6dc267f2ab0ef2b9ba40@news.povray.org>
Darren New <dne### [at] sanrrcom> wrote:
> OK, so I downloaded MS's "express" dev env for C# and C++. I figure it's
> time to practice some C++, learn the ways of the STL, and so on.  Mainly
> because it's that or Erlang or Python, and nobody is going to pay me to code
> any Erlang, and Python looks pretty trivial to learn.

I find myself in the same boat.  I had to do a big class project, so I decided
it was as good a time as any to bite the bullet and actually use C++ and the
STL.  I use FLTK (ugly, but cross-platform and easy!) and OpenGL for the
graphics, but something like Qt would probably also do the trick if you wanted
to try your hand at a cross-platform gui.  There really are a *lot* of
excellent examples when it comes down to it.  I just do a few examples, then
match my problem to that form.

As for specific tutorials, I stumbled upon the C++ annotations, which I've been
very impressed with.  Most examples with 'foo' and 'bar' end up looking trivial
next to real examples, but this really has just about *all* the details.  It
assumes knowledge of C, but I highly recommend it, if only for reference.
Partial template specialization?  The Koenig lookup?  It's all there:

http://www.icce.rug.nl/documents/cplusplus/

 - Ricky


Post a reply to this message

From: triple r
Subject: Re: Learning C++
Date: 18 Dec 2008 00:15:00
Message: <web.4949db1d267f2ab0ef2b9ba40@news.povray.org>
Invisible <voi### [at] devnull> wrote:

> (OTOH, I understand that the raw X Windows bindings are even harder...
> apparently... I guess that's why there's so many toolkits!)

http://www.aerojockey.com/software/ioccc/index.html

This is (honestly) what I based my first X Windows attempt on.  The simulation
is more obfuscated than the code itself.  If you really want quick, economical
coding, IOCCC isn't such a bad place to look--and if all you want is pixel
placement, then X is not as bad as it sounds.  Figure out the endianness, and
you're good to go.

Here's the result of my attempt, complete with volumetric rendering!

* View with ascii - although you have to run it for the form to make sense
* Compile with: cc -lX11 main.c -L/usr/X11R6/lib
* Run with: ./a.out [zoom factor] [resolution] > data
* Playback with: ./a.out [zoom factor] < data
* Just in case they reopen the contest: ./a.out [zoom factor] < main.c
* Careful copying it for that last point--first character should be the tab.
I've attached a copy just for this reason.

                           #include<math.h>
                      #define _(w,Z) u[w][p+Z]
                    #define p a+l*b +V(l +1 )*c+b
                  #define o( x, j,S) U( x,j, l-j,S)
                 #define L(w ,Z ,z )(1- z) *(w)+z*(Z)
                #define M( Z,j) (Z >j ?j:(Z <0? 0:Z))
                #define I m= V( l+1) ;Q= M( a-F[ 0] -\
                (f =(int )(a- F[0]) ),l); E=M (b -F[1\
                ]- (g=( int) (b-F[ 1])), l);W= u[ d]+\
                f+O [1]*g +m*(e=(int )( c-F[2 ] )) ;r\
                 (d +4)=L (L(L( W[0] ,W[1 ],Q),L(W [\
                  l+1],W[ l+2], Q), E),L (L (W [m],\
                            W[m+1],Q),L(W\
                          [m+l+1],W[m+l+2],\
                         Q),E),M(c-F[2]-e,l));
                    #define P(j)U(d,0,3,r(9)=_(9,j\
                  O[2-d]    ) ;h=a;a=b;b=c     ;c=h;)
                            #define T(x ,\
                            j)o (c,x,o(b,\
                            x,o (a,x ,j) ))
                           #include<stdio.h>
                           #define R(x)(x)/2
                          #include<unistd.h>
                          #include<stdlib.h>
                          #define r(w)_(w,0)
                         #define V(w)(w)*(w)
                         #include<X11/Xlib.h>
               #define U(w, Z,j,x)for( w=Z;w< j;w++){x}
      #define K(j)F[j]=R(R(r(j)+_(j,O[d])+_(j,O[j])+_(j,O[d]+O[j])));/*
xtqotSxvwuwtuOyvxrxvwLxwxpxxxJywxoxyxIywynyxyIxwyoxxyIyvxqxwxJyuxtwuvNxtpqr*/
int a,b,c,d,e,f,g,h,i,j,k=6,l,m,Y;double Q,E,*u[012],F[3],*W;char*X,t[80];int
main(int S,char**v){Y=**++v-48;Display*q=XOpenDisplay(0);Window H=RootWindow(
q,0);l=atoi(*(v-2+S));if(S==2){l=(b=getchar())==9?80:b,S-=b==9;}else putchar(
l);GC G=XCreateGC(q,H,0,0);int O[3]={1,l+1,V(l+1)};U(a,0,10,u[a]=calloc(O[1]*
V(l+1),8);)H=XCreateSimpleWindow(q,H,0,0,l*Y,l*Y,0,0,0);if(S-1){T(0,if(V(b-2)
+V(c-R(l))+V(a-R(l))<V(l/10)){r(3)=r(7)=0.02;r(1)=7;})}else{U(e,8,38,gets(t);
)X=t;while(k<=01362){U(m,0,123-*X,U(e,044,44,j=k%79+(l/4-k/79)*O[1]-(p)+e*V(l
+1);_(7,j)=_(3,j)=S;)k++;)S=!S;X++;}T(0,if(a>036&&a<46){if(Q=r(3))U(e,1,3,a+=
16*e;r(3)=Q;a-=16*e;)})S=1;}XMapWindow(q,H);while(G){U(a,0,l,U(b,0,l,if(S-2){
Q=0;U(c,0,l,E=0;f=a;g=b;e=c;while(f&&g-l&&e-l){E+=_(03,f--+g++*O[1]-(p)+(e+=S
==1)*V(l+1));}Q+=r(3)*exp(-E*0.97e2);)e=(M(Q*015410,0377));putchar(e);}else e
=getchar();XSetForeground(q,G,e|e<<8|e<<16|e<<24);U(f,0,Y,U(g,0,Y,XDrawPoint(
q,H,G,a*Y+f,(l-b-1)*Y+g);))))if(S-2){i=1,j=0,k=2;T(0,if(b>2)r(1)+=(r(3)+_(3,l
+1))*3.5/(1.0+4.0*(S==1));if(b<3){d=b;b=l-2+(d==1);r(d)=_(d,-l-1);b=d;})U(d,0
,3,h=k;k=j;j=i;i=h;U(c,1,l-(k!=2),U(b,1,l-(j!=2),U(a,1,l-(i!=2),F[d]=r(d);K(j
)K(i)I))))T(1,U(d,0,3,F[d]=R(r(d)+_(d,O[d]));)I)T(0,r(8)=0;U(d,0,3,r(8)+=_(4+
d,O[d])-r(4+d);))f=80;while(f--){U(a,0,l,U(b,0,l,c=0;P()c=l-1;P(-)))T(1,Q=-r(
8);U(d,0,3,Q+=_(9,O[d])+_(9,-O[d]);)r(9)+=0117*(Q/6-r(9))/0620;)}T(1,U(d,0,3,
if(a)r(d)=r(d+4)-r(9)+_(011,-O[d]);))W=u[3],u[3]=u[7],u[7]=W;}usleep(070731);
if(S-2)putchar(5);else{if(getchar()-05){rewind(stdin);getchar();}}}return 0;}


Post a reply to this message


Attachments:
Download 'main.c.txt' (4 KB)

From: triple r
Subject: Re: Learning C++
Date: 18 Dec 2008 00:50:00
Message: <web.4949e3e9267f2ab0ef2b9ba40@news.povray.org>
"triple_r" <nomail@nomail> wrote:

> * View with ascii - although you have to run it for the form to make sense

Make that fixed-width, before someone points out that it's all ASCII!

 - Ricky


Post a reply to this message

From: Invisible
Subject: Re: Learning C++
Date: 11 Feb 2009 10:19:11
Message: <4992ec6f$1@news.povray.org>
>>> Indeed. Without having a way to talk to the native graphics control
>>> system to set up a drawing surface, OpenGL is no help. (There also seems
>>> to be some disagreement above whether you can use GLUT on Windows...)
>>
>> Again libSDL :)
> 
> It seems to do lots of interesting stuff. Sadly, I can't get the Haskell 
> bindings for it to work. :-(

And now I have it working - but apparently the Haskell binding doesn't 
cover any of the actually useful functions, only the trivial ones. So I 
can find out if the CPU my program is running on supports MMX, but I 
can't, say, draw some pixels on the screen. >_<


Post a reply to this message

From: Darren New
Subject: Re: Learning C++
Date: 11 Feb 2009 13:39:06
Message: <49931b4a$1@news.povray.org>
Invisible wrote:
> And now I have it working

Heh. And I've re-learned C++ now, to a first approximation. Very much the 
kind of language that bites you repeatedly in the butt until you spend 5 
years becoming an expert. I can't see why I would ever want to use it, 
except for going to work someplace that uses it a lot (which is a distinct 
possibility in the near future). I can't even imagine that I'd make 
low-level libraries with it, given it doesn't support the kinds of stuff 
that I need in libraries I write.

-- 
   Darren New, San Diego CA, USA (PST)
   "Ouch ouch ouch!"
   "What's wrong? Noodles too hot?"
   "No, I have Chopstick Tunnel Syndrome."


Post a reply to this message

<<< Previous 10 Messages Goto Initial 10 Messages

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.