[QUOTE=Zinayzen;43862805]I'm totally blanking for some reason. In C++ with constructors, do you need to initialize every variable?
Example:
//Default Constructor Employee(); //Constructor Employee(string, int, int, int, char, string, float, string);
For the actual Constructor do I need to initialize the string/char to null and the int/float to 0, or is that unnecessary? Any time I'd call it I'd be assigning values to all of it anyway, so I'm not sure if that's necessary.
[editline]10th February 2014[/editline]
and if so is there a fast way to do it because i'm lazy and that's a lot of initialization[/QUOTE]
Yes, but you shouldn't just assign in the body of the constructor (this would entail default initialization of the members and then copying, which is wasteful), you should use an initialization list, with optional delegation to other constructors; the syntax is a color (:) followed by an optional delegation to another constructor (for example your default constructor might simply pass reasonable defaults to a more specific constructor) with the syntax `Ctor(ctor_args)`, followed by a comma-separated list of members to initialize in the format `member(ctor_args)`; all together it might look something like:
[code]
// Class Ctor has an int member foo_ and a string member bar_
public Ctor() : Ctor(0, "")
{}
public Ctor(int foo, string bar) : foo_(foo), bar_(bar)
{}
[/code]
This avoids the copying (and allows you to initialize const members, etc.) as the members are actually initialized with the values directly.
yeah i fixed it, thanks
haven't used c++ in like six months so i kept blanking on everything, which is really annoying. I'm okay now though. Guess I need to code when I'm not in school just so I keep the information sharp.
Sooo... does anyone here know how to un-bork a gamemaker game that freezes up when transfering arrays? I made a game that creates a random weapon by using various scripts and by transferring arrays, but after the 3rd or 4th time randomizing it, the entire game freezes up without giving an error message in the console or the debugger.
Okay, so my teacher is making us re-create Flappy Bird's as an in class assignment in Visual Studio 2010, C#, XNA 4.0
My problem is, I can't figure out how to make the randomly generating pipes.
What exactly do you have problems with? Is it about knowing where to spawn the pipes?
Pipes are always some minimum and maximum distance away from each other.
So the X coordinate of the next pipe is "previousPipeX" + Random.Next(50, 70).
The low and high limit are just examples.
Same thing for the pipe height. I'd just randomly calculate the lower pipes height, then add the spacing (where the player has to fly trough) so you get the top-pipes bottom-edge.
If your problem is more generic, not about spawning, but about general handling of the obstacles:
Create a class "Pipe" that contains two Rectangles.
When you want to spawn a new pipe (just before it enters the screen) create it with the way I described above, then add it to a global List<Pipe>
Then every frame you first check if the player collides with any rectangle of any pipe in the list. Then draw every pipe in the list (by the way: this is a good example where batching would be appropriate)
[QUOTE=Exigent;43877888]Okay, so my teacher is making us re-create Flappy Bird's as an in class assignment in Visual Studio 2010, C#, XNA 4.0
My problem is, I can't figure out how to make the randomly generating pipes.[/QUOTE]
Don't make pipes, make holes.
Generate 1 random number, to find the where the top pipe ends, then add 'X' amount of pixels to find where the bottom pipe starts.
Does anyone know of any good resourcesfor achieving realistic water effect in modern openGL?
Another OpenGL question.
Does anyone know any open source 3D games using modern OpenGL? I could use some examples.
I'm trying to implement worms-style grapple hook in a platformer game. It's in gamemaker, so apologies if you're unfamiliar with gml but you should be able to get the gist of my current attempt:
[code]hacc=0
vacc=0
var onfloor = place_meeting(x,y+1,obj_block);
if !onfloor
vacc+=0.1 //gravity
//friction
if hspd>0{
if onfloor
hspd = max(0,hspd-0.3)
else
hspd = max(0,hspd-0.01)
}
else
if hspd<0{
if onfloor
hspd = min(0,hspd+0.3)
else
hspd = min(0,hspd+0.01)
}
//limit vertical speed
if vspd>7
vspd=7
//add acceleration to the speed
vspd+=vacc
hspd+=hacc
//if we're swinging
if rope_x!=-1{
if point_distance(x,y,rope_x,rope_y)>rope_length{ //if we're further away than the rope length
var d = point_direction(rope_x,rope_y,x,y);
x = rope_x + lengthdir_x(rope_length,d)
y = rope_y + lengthdir_y(rope_length,d) //snap back to within the rope length
var xx,yy;
if x<rope_x{ ///compute a unit vector in the direction we want to be going
xx = lengthdir_x(1,d+90)
yy = lengthdir_y(1,d+90)
}
else
{
xx = lengthdir_x(1,d-90)
yy = lengthdir_y(1,d-90)
}
var cs = point_distance(0,0,hspd,vspd)
hacc = xx * cs
vacc = yy * cs
}
}
//do the movement
for(i=0;i<abs(vspd);i++){
if !place_meeting(x,y+sign(vspd),obj_block)
y+=sign(vspd)
else
{
vspd=0
break;
}
}
for(i=0;i<abs(hspd);i++){
if !place_meeting(x+sign(hspd),y,obj_block)
x+=sign(hspd)
else
{
hspd=0
break;
}
}[/code]
Basically, there's vspd and hspd which are the vertical and horizontal speed. We also have vacc and hacc which are added to the vspd and hspd every tick, and then the coordinates are updated using vspd and hspd.
My idea was to basically compute the dot product of the current velocity with a unit vector perpendicular to the rope to get the direction of the velocity in the direction we want to be swinging, but unfortunately that's not worked out. It just slowly approaches the rest position at the below the rope and stops.
How would you go about implementing this?
I can't use angular momentum because it needs to tie in with the rest of my physics, ie must use hspd and vspd.
Hey Guys, got a question about using a DLL (written in C++) with C# - P/Invoke
In my project I have a Matrix class, written in C#. The efficiency of it at the moment is not causing any problems, therefore I am NOT optimising it now ;), but I have read that you may speed up matrix operations by offloading them to functions in a library written with c++. How exactly does one do this? Following the tutorial here: [URL]http://msdn.microsoft.com/en-us/magazine/cc301501.aspx[/URL] I understand how c++ functions can be called from c#, but how do I stucture it if I want to use a class (Matrix) written in c# but only offload the more processor intensive stuff to the library? Should you: Write the martix class in c#, and have its functions (e.g. Invert) as wrappers around the P/Invoke (Passing the matrix data as float[] or whatever), or is there a way to write the struct in c++ and use it in c# (Ive seen C's message struct used for PeekMessage etc.) Thanks in advance :)
Edit: Is this even a good idea anyway ?
[QUOTE=Lemmingz95;43897346][...]
Edit: Is this even a good idea anyway ?[/QUOTE]
No, not in the slightest.
The problem is with the function call efficiency and if you use P/Invoke you get that [I]and[/I] additional overhead for the native call.
[editline]13th February 2014[/editline]
If you want efficient and easy to use maths in .NET you'll have to use a language that allows inlining.
In C# you can somewhat work around it by using reference parameters but it makes the calls really ugly.
[QUOTE=Lemmingz95;43897346]Hey Guys, got a question about using a DLL (written in C++) with C# - P/Invoke
In my project I have a Matrix class, written in C#. The efficiency of it at the moment is not causing any problems, therefore I am NOT optimising it now ;), but I have read that you may speed up matrix operations by offloading them to functions in a library written with c++. How exactly does one do this? Following the tutorial here: [URL]http://msdn.microsoft.com/en-us/magazine/cc301501.aspx[/URL] I understand how c++ functions can be called from c#, but how do I stucture it if I want to use a class (Matrix) written in c# but only offload the more processor intensive stuff to the library? Should you: Write the martix class in c#, and have its functions (e.g. Invert) as wrappers around the P/Invoke (Passing the matrix data as float[] or whatever), or is there a way to write the struct in c++ and use it in c# (Ive seen C's message struct used for PeekMessage etc.) Thanks in advance :)
Edit: Is this even a good idea anyway ?[/QUOTE]
Use a struct on the C# side with the StructLayout attribute and LayoutKind.Sequential. i.e. [url]https://github.com/opentk/opentk/blob/develop/Source/OpenTK/Math/Matrix4.cs[/url]
And a few tips, the overhead of using P/Invoke for something small like matrix math is likely going to make it not that much faster than just doing it in C#, even if you're using SSE instructions and whatnot. It'll also add a lot of complexity to the compilation process of your program, especially if you want to target Linux, OS X, etc. Most graphics/game frameworks in C# do all their matrix math on the C# side and it's pretty fast.
If you want to use SSE instructions in C#, look into Mono.SIMD.
Thanks guys, good to know. Im doing everything C# side now and so far its not a problem. Theres a big list of todos before I can really start stress testing but I can't Imagine itl prove too much of a bottleneck.
[QUOTE=Cold;43882817]Don't make pipes, make holes.
Generate 1 random number, to find the where the top pipe ends, then add 'X' amount of pixels to find where the bottom pipe starts.[/QUOTE]
Could you show me something similar to this? I'm pretty new to coding and I don't understand a lot of the logic/syntax sometimes.
I've made a simple wrapper class for GLFW, but it seems GLEW doesn't like it and glewInit() returns "Missing GL version".
Surprisingly enough using GLFW directly to create a window in the same scope works without problems.
[url]https://gist.github.com/xentec/94ea1e2accffb78ee222[/url] (simplified version)
Any clues?
Hmm, I'm trying to make a system that automatically adds available features to a list depending on what libraries you're linking into it. So far it's completely unsuccessful though, does anyone have any idea on how to do this?
Example code:
[code]
// Bar.hpp - Library A
#pragma once
#include <functional>
#include <list>
DLL_INTERFACE class Bar
{
public:
Bar();
~Bar();
};
list<std::function<void()> > Features;
// Bar.cpp - Library A
#include "Bar.hpp"
list<std::function<void()> > Features;
Bar::Bar()
{
for(auto it = Features.begin(); it != Features.end(); ++it)
(*it)();
}
Bar::~Bar()
{
}
// Foo.hpp - Library B
#pragma once
#include <Bar.hpp>
DLL_INTERFACE class Foo
{
public:
Foo();
~Foo();
}
// Foo.cpp - Library B
#include "Foo.hpp"
namespace {
class HelloWorld
{
public:
HelloWorld()
{
Features.push_back([]() { std::cout << "Hello World"; });
}
} adder;
}
Foo::Foo() { }
Foo::~Foo() { }
// main.cpp - Application
#include <Bar.hpp>
#include <Foo.hpp>
int main()
{
Bar test;
return 0;
}
[/code]
This code doesn't write anything when launched since it seems library B creates its own copy of Features, even though it's a static variable that's defined and implemented inside library A.
Any other ideas on how to do this?
[QUOTE=ace13;43908195]Hmm, I'm trying to make a system that automatically adds available features to a list depending on what libraries you're linking into it. So far it's completely unsuccessful though, does anyone have any idea on how to do this?
Example code:
[code]
// Bar.hpp - Library A
#pragma once
#include <functional>
#include <list>
DLL_INTERFACE class Bar
{
public:
Bar();
~Bar();
};
list<std::function<void()> > Features;
// Bar.cpp - Library A
#include "Bar.hpp"
list<std::function<void()> > Features;
Bar::Bar()
{
for(auto it = Features.begin(); it != Features.end(); ++it)
(*it)();
}
Bar::~Bar()
{
}
// Foo.hpp - Library B
#pragma once
#include <Bar.hpp>
DLL_INTERFACE class Foo
{
public:
Foo();
~Foo();
}
// Foo.cpp - Library B
#include "Foo.hpp"
namespace {
class HelloWorld
{
public:
HelloWorld()
{
Features.push_back([]() { std::cout << "Hello World"; });
}
} adder;
}
Foo::Foo() { }
Foo::~Foo() { }
// main.cpp - Application
#include <Bar.hpp>
#include <Foo.hpp>
int main()
{
Bar test;
return 0;
}
[/code]
This code doesn't write anything when launched since it seems library B creates its own copy of Features, even though it's a static variable that's defined and implemented inside library A.
Any other ideas on how to do this?[/QUOTE]
Have you tried to declare Features as extern in your Bar.hpp?
[QUOTE=Dienes;43908366]Have you tried to declare Features as extern in your Bar.hpp?[/QUOTE]
Well, I managed to "solve" it.
Ended up creating a few wrapper functions and exporting one of those in external "C". I'm going to assume it was a name wrangling issue since the variable I was really exporting was a unordered map against a class enum and a function that takes a class reference.
Thanks for the help nonetheless, you actually gave me the ideas I needed to solve it.
Hi, I am new to pthreads on Linux and I am trying to create a semaphore (sem_init). However, I've noticed that sem_init fails when I do not run the program as a super-user. It gives me a EPERM error. Is there a way to create a semaphore without requiring these privileges? I do not want to force users to run the program with sudo.
I have also tried sem_open, but it basically gave me the same trouble.
For reference, my operating system is Xubuntu (Ubuntu x86-64), my IDE is Code::Blocks.
[QUOTE=Natrox;43921479]Hi, I am new to pthreads on Linux and I am trying to create a semaphore (sem_init). However, I've noticed that sem_init fails when I do not run the program as a super-user. It gives me a EPERM error. Is there a way to create a semaphore without requiring these privileges? I do not want to force users to run the program with sudo.
I have also tried sem_open, but it basically gave me the same trouble.
For reference, my operating system is Xubuntu (Ubuntu x86-64), my IDE is Code::Blocks.[/QUOTE]
Works fine over here:
[cpp]
#include <stdio.h>
#include <semaphore.h>
int main(int argc, char *argv[])
{
sem_t mysema;
printf("%d\n", sem_init(&mysema, 0, 10));
printf("%d\n", sem_destroy(&mysema));
return 0;
}
[/cpp]
clang 3.3, fedora 20, glibc 2.18-12
Bonus question: When executing a method for several instances of said class that uses the Random class to move a object across the x axis, they all seem to move at the same increments and keep going.
[code]
private void button2_Click(object sender, EventArgs e)
{
int winner = 0;
while (winner == 0)
{
for (int i = 0; i < Dogs.Length; i++)
{
Dogs[i].Run();
if (Dogs[i].Location > 550)
winner = i + 1;
}
Application.DoEvents();
System.Threading.Thread.Sleep(1000);
}
if (winner > 0)
for (int x = 0; x < Bettors.Length; x++)
Bettors[x].Collect(winner);
for (int y = 0; y < Bettors.Length; y++)
Bettors[y].UpdateLabels();
}
[/code]
[QUOTE=LtKyle2;43922829]Bonus question: When executing a method for several instances of said class that uses the Random class to move a object across the x axis, they all seem to move at the same increments and keep going.
[code]
private void button2_Click(object sender, EventArgs e)
{
int winner = 0;
while (winner == 0)
{
for (int i = 0; i < Dogs.Length; i++)
{
Dogs[i].Run();
if (Dogs[i].Location > 550)
winner = i + 1;
}
Application.DoEvents();
System.Threading.Thread.Sleep(1000);
}
if (winner > 0)
for (int x = 0; x < Bettors.Length; x++)
Bettors[x].Collect(winner);
for (int y = 0; y < Bettors.Length; y++)
Bettors[y].UpdateLabels();
}
[/code][/QUOTE]
If you create a Random class per Dog class, then they will all default to the current time as the seed. So I guess you're creating them all at the same time, which gives them the same seed and therefor will generate the same random sequence.
So to fix the problem you should either create every instance with a unique seed or have one instance that they're all using.
So I'm a masochist and decided to try and write a binary module for gmod in C#. The problem I'm having is that it fails to be loading because of missing dependencies. This seems to be a relatively frequent error with Windows 8.1. Does anyone know how to fix or even just work around this?
[img_thumb]http://puu.sh/6YtGD[/img_thumb]
[QUOTE=Agent766;43934453]-null-[/QUOTE]
Check at what i did. Also i don't know what you're talking about when saying it's relatively frequent error with Win 8.1
[url]https://github.com/cartman300/GMOD-NET-API[/url]
It looks like it's frequent based on this and related google searches: [url]http://answers.microsoft.com/en-us/windows/forum/windows8_1-performance/32-bit-application-fails-to-start-after-81-upgrade/b825723e-e2a2-4c8f-bd1f-10446a5d7059[/url]
I'm working on box2dweb as part of Gamvas and I can't get reversing/no gravity to work. It doesn't have a setgravityscale as it is based on an older box2d so I have been trying to use the applyforce method. But when I do use this, it freaks the hell out and slows down the game a ton, with the boxes disappearing except for one.
[CODE]this.actor.body.ApplyForce(this.actor.body.GetMass()* -this.actor.body.GetWorld().GetGravity(), this.actor.body.GetPosition());[/CODE]
That code should make the "actor" have no gravity, but it just freaks out.
How do I get it working properly?
I'm doing a data mining module and was wondering can anybody suggest a beginner project for something to mine on? I would like to do it on something to do with movies or something but can't think of any ideas
Does anyone have any good resources on building abstract syntax trees? I'm trying to implement a simple scripting language and I'm finding it pretty difficult to conceptualise ASTs. I have a workable lexer to take my script and convert it to a list of tokens, but parsing those into an AST is eluding me.
[QUOTE=Over-Run;43939958]I'm doing a data mining module and was wondering can anybody suggest a beginner project for something to mine on? I would like to do it on something to do with movies or something but can't think of any ideas[/QUOTE]
[url]http://bugra.github.io/work/notes/2014-02-15/imdb-top-100K-movies-analysis-in-depth-part-1/[/url]
(So maybe do some basic web scraping on IMDb?)
Hello,
my brain isnt working well today i have a Problem and dont find a solution i Programm with C#
I have a angle=-90 to angle+90
And Need a percent calculation for the right angle.
For example i have These values
Max. Hours= 150
Used Hours= 97
Now i Need the right angle for the percent of used hours.
How can i solve that?
Sorry, you need to Log In to post a reply to this thread.