After I tried to stop being a quitter I continued with WinSock and I have a little problem. It says SockAddr is undefined, while this should be defined in your standard headers. Here is the current code. It is obviously not done:
[cpp]#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <WinSock.h>
#include <windows.h>
//#include <WinSock2.h>
#include <WS2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
// #pragma comment (lib, "Mswsock.lib")
int main()
{
WSADATA WsaDat;
if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
{
printf("WSA Initialization failed.");
}
SOCKET Socket;
Socket = socket(AF_INET, SOCK_STREAM, 0);
if (Socket == INVALID_SOCKET)
{
printf("Socket Creation Failed");
}
SockAddr.sin_port = AF_INET;
[/cpp]
The last line is the problem.
[QUOTE=Asgard;32080414]winsock[/QUOTE]
You haven't defined SockAddr yet.
[cpp]
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <WinSock.h>
#include <windows.h>
//#include <WinSock2.h>
#include <WS2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
// #pragma comment (lib, "Mswsock.lib")
int main()
{
WSADATA WsaDat;
if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
{
printf("WSA Initialization failed.");
}
SOCKET Socket;
Socket = socket(AF_INET, SOCK_STREAM, 0);
if (Socket == INVALID_SOCKET)
{
printf("Socket Creation Failed");
}
SOCKADDR_IN SockAddr;
SockAddr.sin_port = AF_INET;
[/cpp]
[QUOTE=MallocNull;32080611]You haven't defined SockAddr yet.
[cpp]
#undef UNICODE
#define WIN32_LEAN_AND_MEAN
#include <WinSock.h>
#include <windows.h>
//#include <WinSock2.h>
#include <WS2tcpip.h>
#include <stdlib.h>
#include <stdio.h>
// Need to link with Ws2_32.lib
#pragma comment (lib, "Ws2_32.lib")
// #pragma comment (lib, "Mswsock.lib")
int main()
{
WSADATA WsaDat;
if (WSAStartup(MAKEWORD(1, 1), &WsaDat) != 0)
{
printf("WSA Initialization failed.");
}
SOCKET Socket;
Socket = socket(AF_INET, SOCK_STREAM, 0);
if (Socket == INVALID_SOCKET)
{
printf("Socket Creation Failed");
}
SOCKADDR_IN SockAddr;
SockAddr.sin_port = AF_INET;
[/cpp][/QUOTE]
Derp. Thanks. Should've spotted that one myself :v:
[QUOTE=Richy19;32080159]words[/QUOTE]
Virtual methods only work if you're using pointers. Specifically, pointers to some base class.
[cpp]class Base
{
public:
virtual void func1()
{
cout << "Base function!" << endl;
}
void func2() // NOTICE: not virtual
{
cout << "Base function!" << endl;
}
};
class Child : public Base
{
public:
void func1() // NOTICE: you do not need "virtual" on the child class implementation
{
cout << "Child function!" << endl;
}
void func2()
{
cout << "Child function!" << endl;
}
};
int main()
{
vector<Base*> objects;
objects.push_back(new Base());
objects.push_back(new Child());
for (unsigned int i = 0; i < objects.size(); i++)
{
objects[i]->func1();
objects[i]->func2();
}
}[/cpp]
Output:
[quote]Base function!
Base function!
Child function!
Base function![/quote]
So for overwriting methods from the base class with a method in the child class I dont have to do anything?
[editline]2nd September 2011[/editline]
If im not using pointers that it
Well, you could always control function pointers yourself.
If you're not using pointers (or references) then there's no need for virtual because the exact type of the object is known at compile time, so the compiler knows which class's function to call.
But imagine you have a function that takes an argument of type Base* or Base&, and something calls it with an instance of Derived as the parameter. The compiler doesn't know, when it compiles the function, what the actual type of the parameter will be. The virtual keyword makes the compiler produce code that checks the type of the parameter [i]at runtime[/i] so that it'll call the Derived version of the function when the parameter is a Derived instance.
Anyone know what an "Undefined Reference Typeinfo for..." is about? I get a few when using Gwen
[QUOTE=Richy19;32080159]so from what i understand it allows you to do:
class a
{
virtual something();
}
class b : public a
{
virtual something(); // does different stuff
}
class c : public a
{
virtual something(); // does different stuff
}
vector<a> list;
list.add(b);
list,add(c);
list[0].something(); // calls b something() because it is virtual
list[1].soomething(); // calls c something() because its virtual[/QUOTE]
I think you got the point kinda right, it's just that you can't just use a class as if it were an object. You'd have to create an instance of b and c to add to the vector first. Also you have to use pointers for it to work like said above.
[url]http://dl.dropbox.com/u/21571661/congl.7z[/url]
Try out my 3D thingy
It's mostly broken, but still kinda looks 3D.
Source included, C#
I want to make something, that when a string is one thing, it automatically swaps.
Instead of a bunch of if...else...if...else i wanted something like this:
[code]
const char cEntitySwap[2][2][64] =
{
"func_derp", "asshole"
"func_derp2","another_asshole"
};
[/code]
So when a string equals "func_derp" i can grab "asshole".
But this is wrong [i]obviously[/i]. How would I go about doing this?
[QUOTE=Drak_Thing;32096927]I want to make something, that when a string is one thing, it automatically swaps.
Instead of a bunch of if...else...if...else i wanted something like this:
[code]
const char cEntitySwap[2][2][64] =
{
"func_derp", "asshole"
"func_derp2","another_asshole"
};
[/code]
So when a string equals "func_derp" i can grab "asshole".
But this is wrong [i]obviously[/i]. How would I go about doing this?[/QUOTE]
I'm not completely sure what you mean here, but from my understanding, you're looking for an std::map<std::string, std::string>
[cpp]
std::map<std::string, std::string> cEntitySwap;
cEntitySwap["func_derp"] = "asshole";
cEntitySwap["func_derp2"] = "another_asshole";
std::string entityName;
// Get entityName from something here.
if (cEntitySwap.find(entityName) != cEntitySwap.end()) {
entityName = cEntitySwap[entityName];
}
[/cpp]
[QUOTE=thf;32097063]I'm not completely sure what you mean here, but from my understanding, you're looking for an std::map<std::string, std::string>
[cpp]
std::map<std::string, std::string> cEntitySwap;
cEntitySwap["func_derp"] = "asshole";
cEntitySwap["func_derp2"] = "another_asshole";
std::string entityName;
// Get entityName from something here.
if (cEntitySwap.find(entityName) != cEntitySwap.end()) {
entityName = cEntitySwap[entityName];
}
[/cpp][/QUOTE]
That's perfect. But is there a method without std?
[QUOTE=Drak_Thing;32098018]That's perfect. But is there a method without std?[/QUOTE]
Write your own container
[QUOTE=Drak_Thing;32098018]That's perfect. But is there a method without std?[/QUOTE]
using namespace std;
now there is no std, it is magic :O
How do I make runnable JARs with Eclipse? Is there any way to make it at all?
I'm trying to make a game and I need the paddle on the right to follow the ball, but it only keeps up until it goes outside the window. I understand the error I have made in the code below but I need help making the paddle follow the ball.
Here is the code I was working on to make the paddle do that:
(I used VS2010 and SFML 2.)
[cpp]
if (sBall.GetPosition().y > paddle2.GetPosition().y)
{
sf::Vector2f pos = paddle2.GetPosition();
paddle2.SetPosition(pos.x + 0, pos.y - paddle2Speed);
}
if (sBall.GetPosition().y < paddle2.GetPosition().y)
{
sf::Vector2f pos = paddle2.GetPosition();
paddle2.SetPosition(pos.x + 0, pos.y + paddle2Speed);
}
[/cpp]
I just started learning c++ and I was wondering if there was a percent random function. An example would be you do rand()%10 and 20 percent of the time you get a 0 and for the rest of the numbers it would be about a 8.8 percent chance. The way I am doing it now is by using if functions and the code is getting long. Is there a shorter way of doing this or is there a function for it?
[QUOTE=Dark7533;32105129]I just started learning c++ and I was wondering if there was a percent random function. An example would be you do rand()%10 and 20 percent of the time you get a 0 and for the rest of the numbers it would be about a 8.8 percent chance. The way I am doing it now is by using if functions and the code is getting long. Is there a shorter way of doing this or is there a function for it?[/QUOTE]
It's not perfect, but you could try seeding with the current timestamp:
[cpp]
#include <iostream>
#include <cstdlib>
#include <ctime>
// Generate 5 random numbers
std::srand(std::time(nullptr));
for (int i = 0; i < 5; ++i) {
std::cout << std::rand() % 10 << std::endl;
}
[/cpp]
I want to make a piece of text move up like credits at the end of a movie or something. I included windows.h and used the Sleep() function. The problem is that when the program starts the is a ~4 second pause before the text starts "moving" up. After the first wait the loop works fine, with the needed delay of 200ms.
[cpp]#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
cout << "\n\n\n\n\n\n\n\n\nTest";
for (int i = 1; i < 25; i ++)
{
cout << "\n";
Sleep(200);
}
cin.get();
}[/cpp]
[QUOTE=WhatTheEf;32106456]I want to make a piece of text move up like credits at the end of a movie or something. I included windows.h and used the Sleep() function. The problem is that when the program starts the is a ~4 second pause before the text starts "moving" up. After the first wait the loop works fine, with the needed delay of 200ms.
[cpp]#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
cout << "\n\n\n\n\n\n\n\n\nTest";
for (int i = 1; i < 25; i ++)
{
cout << "\n";
Sleep(200);
}
cin.get();
}[/cpp][/QUOTE]
You could try making your own sleep function:
[cpp]
#include <iostream>
#include <ctime>
using namespace std;
void sleep(unsigned ms)
{
clock_t goal = clock() + (ms * CLOCKS_PER_SEC / 1000);
while (clock() < goal)
;
}
int main()
{
cout << "\n\n\n\n\n\n\n\n\nTest";
for (int i = 1; i < 25; ++i) {
cout << "\n";
sleep(200);
}
cin.get();
}
[/cpp]
[QUOTE=thf;32106559]You could try making your own sleep function:
[cpp]
#include <iostream>
#include <ctime>
using namespace std;
void sleep(unsigned ms)
{
clock_t goal = clock() + (ms * CLOCKS_PER_SEC / 1000);
while (clock() < goal)
;
}
int main()
{
cout << "\n\n\n\n\n\n\n\n\nTest";
for (int i = 1; i < 25; ++i) {
cout << "\n";
sleep(200);
}
cin.get();
}
[/cpp][/QUOTE]
The first delay is still there.
[QUOTE=WhatTheEf;32106570]The first delay is still there.[/QUOTE]
If I understood you right, the program shows the text before that 4 second delay, right?
[QUOTE=thf;32106585]If I understood you right, the program shows the text before that 4 second delay, right?[/QUOTE]
Indeed.
[QUOTE=WhatTheEf;32106597]Indeed.[/QUOTE]
Strange, it doesn't do that for me when I run it.
[QUOTE=thf;32106633]Strange, it doesn't do that for me when I run it.[/QUOTE]
Here, I made a video:
[media]http://www.youtube.com/watch?v=Cc1yUjrwXwg[/media]
[QUOTE=WhatTheEf;32106680]Here, I made a video:
[media]http://www.youtube.com/watch?v=Cc1yUjrwXwg[/media][/QUOTE]
And the exact same happens when you use my sleep function? Try running the exe file on its own to not debug it, because there tends to be some delays when you're debugging stuff there.
Nevermind. The delay is there because there are free spaces below the text and it takes a while to get at the end of the window. Here's a code that works ok:
[cpp]#include <iostream>
#include <windows.h>
using namespace std;
int main()
{
cout << "\n\n\n\n\n\n\n\n\nTest";
cout << "\n\n\n\n\n\n\n\n\n\n\n\n\n";
for (int i = 1; i < 15; i ++)
{
cout << "\n";
Sleep(200);
}
cin.get();
} for (int i = 0; i < 25; i ++)
{
cout << "\n";
Sleep(150);
}
cin.get();
}[/cpp]
-snip- wrong thread
Reposting:
So I'm still working on my 3d engine that I'm creating from scratch and I just had an attempt at getting the mouse to rotate my camera.
Well, it 'kinda' works. This is the code that I use:
[code]
glm::vec2 cursorpos = mouse.GetMousePos();
GLint viewport[4];
glGetIntegerv(GL_VIEWPORT, viewport);
Camera.AddYaw( ( cursorpos.x - (viewport[2] / 2) ) * 0.1f );
mouse.SetMousePos( glm::vec2( viewport[2] / 2, viewport[3] / 2 ) );
[/code]
This works, that if I just move my mouse from side to side the camera rotates too. However, it has these weird spikes that happens quite often. If I just keep slowly moving my mouse, all of a sudden it will spike quickly and rotate the camera QUITE a distance. Usually so much that it practically makes a 180 degree turn.
My question is, what causes this? I've tried multiplying it by deltatime, changing that 0.1f constant etc... I really can't figure out what causes this. Help?
I uploaded the .exe here if anybody wants to see my problem:
[url]http://www.2shared.com/file/6DbFQnsA/Release.html?[/url]
You can move the mouse left & right to rotate the camera, or you can use the arrow keys to rotate the camera to the sides or up and down. In there, there's 2 blue rectangles. You'll see them if you press left arrow a couple of times. By the way, I am quite aware of the fact that escape can't close it etc. Use alt+f4 to close it.
Perhaps anyone can direct me towards a tutorial / article about mouse input?
Sorry, you need to Log In to post a reply to this thread.