I am trying to get OpenGL-ES working on a Raspberry Pi, but so far no luck. I compiled SDL 2.0.3 from source with this, as the version in Rasbian is missing Raspberry Pi support:
./configure --prefix=/home/pi/run/SDL2-2.0.3/ \
--disable-video-x11 \
--disable-pulseaudio \
--disable-esd \
--disable-video-opengl
The code below should create a OpenGL context and clear the screen to red. When I run the code, the Raspberry Pi is switching video modes, but the screen is turning black instead of red and the calls to glGetString(GL_VERSION) and Co. return NULL which would indicate that something is wrong with the GL context creation.
#include <SDL.h>
#include <SDL_opengles2.h>
#include <iostream>
void print_gl_string(GLenum name)
{
const GLubyte* ret = glGetString(name);
if (ret == 0)
{
std::cerr << "error getting string: " << name << std::endl;
}
else
{
std::cerr << name << ": " << ret << std::endl;
}
}
void set_gl_attribute(SDL_GLattr attr, int value)
{
if (SDL_GL_SetAttribute(attr, value) != 0)
{
std::cerr << "SDL_GL_SetAttribute(" << attr << ", " << value << ") failed: " << SDL_GetError() << std::endl;
}
}
int main()
{
if (SDL_Init(SDL_INIT_VIDEO) != 0)
{
std::cerr << "SDL_Init() failed: " << SDL_GetError() << std::endl;
exit(EXIT_FAILURE);
}
SDL_DisplayMode videomode;
if (SDL_GetCurrentDisplayMode (0, &videomode) != 0)
{
std::cerr << "Error getting current display mode: " << SDL_GetError() << std::endl;
exit(EXIT_FAILURE);
}
std::cout << "Current screen mode: " << videomode.w << "x" << videomode.h << std::endl;
set_gl_attribute(SDL_GL_RED_SIZE, 5);
set_gl_attribute(SDL_GL_GREEN_SIZE, 6);
set_gl_attribute(SDL_GL_BLUE_SIZE, 5);
//set_gl_attribute(SDL_GL_DEPTH_SIZE, 8);
set_gl_attribute(SDL_GL_DOUBLEBUFFER, 1);
set_gl_attribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
set_gl_attribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
set_gl_attribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_Window* window = SDL_CreateWindow("Minimal SDL2 Example",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
720, 576,
SDL_WINDOW_OPENGL);
if (!window)
{
std::cerr << "Could not create window: " << SDL_GetError() << std::endl;
exit(EXIT_FAILURE);
}
SDL_GLContext gl_context = SDL_GL_CreateContext(window);
print_gl_string(GL_RENDERER);
print_gl_string(GL_SHADING_LANGUAGE_VERSION);
print_gl_string(GL_VERSION);
print_gl_string(GL_EXTENSIONS);
glClearColor(1.0, 0.0, 0.0, 1.0);
glClear(GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(window);
SDL_Delay(5000);
SDL_GL_DeleteContext(gl_context);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
The problem turned out to not be in the code, but in the library path. A simple -L/opt/vc/lib/ added to the compile command line fixed it. Without that the compiler would pick:
/usr/lib/arm-linux-gnueabihf/libGLESv2.so.2
While the right one would be (use ldd to check):
/opt/vc/lib/libGLESv2.so
Related
This is my first time asking a question, so apologies if it is not done 100%:
I have a class which saves and loads a binary file with a specific data structure.
If the program creates an instance of the class, save the binary file, and creates another instance of the class to load/read the binary file consequently, everything seems 100% correct.
However, if I run the program to save the binary file and then run it again to load/read that binary file, it gives me a segmentation fault at the end.
The program still does everything it needs to do before the segmentation fault, except deconstructing the class at the end (obviously).
It looks like my allocation of the memory is not correct, but I am not sure where I am going wrong.
A simplified version of the code follow (also here: https://github.com/LenteDreyer/Tests.git )
Can someone see where I am going wrong?
class header file that save/loads the file
#ifndef __TESTS_MAP_HH__
#define __TESTS_MAP_HH__
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <algorithm>
#include <sstream>
typedef struct test_struct{
bool test_bool;
double test_double;
std::vector<double> test_vector;
} test_struct_t;
class map
{
private:
std::string m_path, m_str;
double m_double;
test_struct m_struct;
public:
map(const std::string& a_id);
void set_str(std::string a_str);
void set_double(double a_double);
void set_struct(test_struct a_struct);
void load_file();
void save_file() const;
void show_file() const;
~map();
};
#endif //__TESTS_MAP_HH__
class source file that save/loads the binary file
#include "map.hh"
map::map(const std::string& a_id)
{
m_path = a_id + ".bin";
m_str = "none";
m_double = 0.0;
m_struct = {false, 0.0};
}
void map::set_str(std::string a_str){
m_str = a_str;
}
void map::set_double(double a_double){
m_double = a_double;
}
void map::set_struct(test_struct a_struct){
m_struct = a_struct;
}
void map::load_file(){
std::ifstream l_inF;
l_inF.open(m_path.c_str(), std::ios::binary | std::ios::in);
l_inF.read((char*)&m_double,sizeof(double));
l_inF.read((char*)&m_struct,sizeof(test_struct_t));
size_t str_size;
l_inF.read((char*)&str_size, sizeof(str_size));
m_str.resize(str_size);
l_inF.read((char*)&m_str[0], str_size);
l_inF.close();
}
void map::save_file() const{
std::ofstream l_outF;
l_outF.open(m_path.c_str(), std::ios::binary | std::ios::out);
l_outF.write((char*)&m_double,sizeof(double));
l_outF.write((char*)&m_struct,sizeof(test_struct_t));
size_t str_size = m_str.size();
l_outF.write((char*)&str_size, sizeof(str_size));
l_outF.write((char*)&m_str[0], str_size);
l_outF.close();
}
void map::show_file() const{
std::cout << ">>-----------------------------------------------" << std::endl;
std::cout << ">> double : " << m_double << std::endl;
std::cout << ">> double : " << m_double << std::endl;
std::cout << ">> struct.bool : " << m_struct.test_bool << std::endl;
std::cout << ">> struct.double : " << m_struct.test_double << std::endl;
std::cout << ">> struct.vector : " << "size = " << m_struct.test_vector.size() << std::endl;
std::cout << ">> string : " << m_str << std::endl;
std::cout << ">>-----------------------------------------------" << std::endl;
}
map::~map(){}
main function case 1 works, and case 2 gives the segmentation fault.
#include "map.hh"
int main(int argc, char const *argv[])
{
std::string id = "mapfile";
int action = 0;
if(argc > 1) action = std::stoi(argv[1]);
else {
std::string input;
std::cout << "Enter case (1 or 2): ";
std::cin >> input;
action = std::stoi(input);
}
switch (action)
{
case 1:
{
// This works 100% (no errors and it saves/reads class perfectly)
std::vector<double> l_vect = {0.1, 0.0, 0.6};
test_struct save_struct = {true, 5.0, l_vect};
map test_save(id);
test_save.show_file();
test_save.set_double(8.0);
test_save.set_str("save this string");
test_save.set_struct(save_struct);
test_save.show_file();
test_save.save_file();
map test_load(id);
test_load.load_file();
test_load.show_file();
}
break;
case 2:
{
// gives segmentation error at the end of the program
map test_load(id);
test_load.load_file();
test_load.show_file();
}
break;
default:
break;
}
return 0;
}
I am implementing a tcp server and client using secure sockets (Poco::Net::SecureServerSocket), I attach here the code I am using:
void serverClientTest()
{
try {
Poco::Net::initializeSSL();
// Socket server
Poco::Net::Context::Ptr ptrContext =
new Poco::Net::Context(Poco::Net::Context::TLS_SERVER_USE,
"./cert4/myKey.pem",
"./cert4/myCert.pem",
"./cert4/myCert.pem",
Poco::Net::Context::VERIFY_ONCE);
Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> ptrCert = new Poco::Net::AcceptCertificateHandler(true);
Poco::SharedPtr<Poco::Net::PrivateKeyPassphraseHandler> ptrPrivateKeyPassphraseHandler;
ptrPrivateKeyPassphraseHandler = new Poco::Net::KeyConsoleHandler(true);
Poco::Net::SSLManager::instance().initializeServer(ptrPrivateKeyPassphraseHandler, ptrCert, ptrContext);
Poco::Net::SocketAddress serverAddress("0.0.0.0", 8085);
Poco::Net::SecureServerSocket serverSecureSocket(serverAddress);
Poco::Net::TCPServer srv(new Poco::Net::TCPServerConnectionFactoryImpl<EchoConnection>(), serverSecureSocket);
srv.start();
Poco::Net::Context::Ptr ptrContext2 =
new Poco::Net::Context(Poco::Net::Context::TLS_CLIENT_USE,
"./cert4/myKey.pem",
"./cert4/myCert.pem",
"./cert4/myCert.pem",
Poco::Net::Context::VERIFY_ONCE);
Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> ptrCert2 = new Poco::Net::AcceptCertificateHandler(true);
Poco::SharedPtr<Poco::Net::PrivateKeyPassphraseHandler>ptrPrivateKeyPassphraseHandler2(new Poco::Net::KeyConsoleHandler(false));
Poco::Net::SSLManager::instance().initializeClient(ptrPrivateKeyPassphraseHandler2, ptrCert2, ptrContext2);
Poco::Net::SocketAddress sa("127.0.0.1", 8085);
Poco::Net::SecureStreamSocket ss1(sa);
std::string data("TEST TEST");
int retSend = ss1.sendBytes(data.data(), (int) data.size());
if (retSend>0)
{
std::cout << "buffer -> : " << data.data() << std::endl;
char buffer[1024];
memset(buffer, '\0', 1024);
int retRecv = ss1.receiveBytes(buffer, sizeof(buffer));
if (retRecv > 0)
{
std::cout << "buffer <- : " << buffer << std::endl;
}
else
{
std::cout << "ERROR: recv " << retRecv << std::endl;
}
}
ss1.close();
}
catch (Poco::Exception& ex)
{
std::cout << "!! EXCEPTION "<< ex.displayText() << std::endl;
}
}
//[....]
class EchoConnection: public Poco::Net::TCPServerConnection
{
public:
EchoConnection(const Poco::Net::StreamSocket& s): Poco::Net::TCPServerConnection(s){}
void run()
{
Poco::Net::StreamSocket& ss = socket();
std::cout << "connection from client: " << ss.address() << std::endl;
try
{
// ...
}
catch (Poco::Exception& exc)
{
std::cerr << "--------------- EchoConnection: " << exc.displayText() << std::endl;
}
}
};
I would like the server to close the connection if the client certificate is not known to the server, but it happens
which, even with the context:
Poco::Net::Context::Ptr ptrContext2 =
new Poco::Net::Context(Poco::Net::Context::TLS_CLIENT_USE,
"",
"",
"./cert4/myCert.pem",
Poco::Net::Context::VERIFY_ONCE);
Thanks to anyone who can help me out.
void serverClientTest()
{
try {
Poco::Net::initializeSSL();
// Socket server
Poco::Net::Context::Ptr ptrContext =
new Poco::Net::Context(Poco::Net::Context::SERVER_USE,
"./server.key",
"./server.crt",
"./ca.pem",
Poco::Net::Context::VERIFY_STRICT,
9,
false,
"ALL:!ADH:!LOW:!EXP:!MD5:#STRENGTH");
Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> ptrCert = new Poco::Net::AcceptCertificateHandler(true);
Poco::SharedPtr<Poco::Net::PrivateKeyPassphraseHandler> ptrPrivateKeyPassphraseHandler;
ptrPrivateKeyPassphraseHandler = new Poco::Net::KeyConsoleHandler(true);
Poco::Net::SSLManager::instance().initializeServer(ptrPrivateKeyPassphraseHandler, ptrCert, ptrContext);
Poco::Net::SocketAddress serverAddress("0.0.0.0", 8085);
Poco::Net::SecureServerSocket serverSecureSocket(serverAddress);
Poco::Net::TCPServer srv(new Poco::Net::TCPServerConnectionFactoryImpl<EchoConnection>(), serverSecureSocket);
srv.start();
Poco::Net::Context::Ptr ptrContext2 =
new Poco::Net::Context(Poco::Net::Context::CLIENT_USE,
"./client.key",
"./client.crt",
"./ca.pem",
Poco::Net::Context::VERIFY_STRICT,
9,
true,
"ALL:!ADH:!LOW:!EXP:!MD5:#STRENGTH");
Poco::SharedPtr<Poco::Net::InvalidCertificateHandler> ptrCert2 = new Poco::Net::AcceptCertificateHandler(true);
Poco::SharedPtr<Poco::Net::PrivateKeyPassphraseHandler>ptrPrivateKeyPassphraseHandler2(new Poco::Net::KeyConsoleHandler(false));
Poco::Net::SSLManager::instance().initializeClient(ptrPrivateKeyPassphraseHandler2, ptrCert2, ptrContext2);
Poco::Net::SocketAddress sa("127.0.0.1", 8085);
Poco::Net::SecureStreamSocket ss1(sa);
std::string data("TEST");
int retSend = ss1.sendBytes(data.data(), (int) data.size());
if (retSend>0)
{
char buffer[1024];
memset(buffer, '\0', 1024);
int retRecv = ss1.receiveBytes(buffer, sizeof(buffer));
if (retRecv > 0)
{
std::cout << "buffer <-: " << buffer << std::endl;
}
else
{
std::cout << "ERROR: " << retRecv << std::endl;
}
}
ss1.close();
}
catch (Poco::Exception& ex)
{
std::cout << ex.displayText() << std::endl;
}
}
I am struggling to understand how to properly use pass by value and reference in this program. It keeps saying identifier is undefined when I put the Int in the main function. But when it's out of the main function it works fine. I can't use it outside of the main function otherwise it's an automatic 0. Am I just missing code that identifies it?
I didn't explain the program very well. It's essentially the Monty Hall problem but with boxes. The user then tries to guess the right box. The biggest issue I had was getting the non prize box to appear to the user in the output.
I am very new to coding so I am probably overlooking something.
#include <stdlib.h>
#include <iomanip>
#include <iostream>
#include <time.h>
#include <string>
using namespace std;
/*******************************************************************************
* Function Name: main()
* Parameters: None
* Return Value: int
* Purpose: To execute the main function of the program. Performs loops and takes user input to execute the game.
*****************************************************************************/
void BoxCheck(int);
void PrizeBox(int&);
//int UserGuess = 0;
int main()
{
int UserChoice;
int UserGuess;
int BoxReveal;
int PrizeB;
// int Prize;
//char Switch;
cout << "Wellcome to Pandora's Prize!" << endl;
cout << "Infront of you there are three doors, and behind one of them is the grand prize!" << endl;
cout << "But in the the other two they contain a stink bomb." << endl;
cout << "It's your job to guess which one has the prize behind it." << endl;
cout << "Door 1?" << endl;
cout << "Door 2?" << endl;
cout << "Door 3?" << endl;
PrizeBox(PrizeB);
cin >> UserGuess;
BoxReveal != (PrizeB || UserGuess);
// cout << "The host revealed box number " << BoxReveal << " ." << endl;
//if (true)
//{
if (UserGuess == 1)
{
cout << "You picked box 1." << endl;
cout << "The host revealed box number " << BoxReveal << " ." << endl;
cout << "Would you like to stay with box number 1 or switch." << endl;
//cin >> UserChoice;
}
else if (UserGuess == 2)
{
cout << "You picked box 2." << endl;
cout << "The host revealed box number " << BoxReveal << " ." << endl;
cout << "Would you like to stay with box number 2 or switch." << endl;
//cin >> UserChoice;
}
else if (UserGuess == 3)
{
cout << "You picked box 3." << endl;
cout << "The host revealed box number " << BoxReveal << " ." << endl;
cout << "It contains a red snapper!" << endl;
cout << "Would you like to stay with box number 3 or switch." << endl;
//cin >> UserChoice;
}
else
{
cout << "This isn't a number associated with a box. Try again." << endl;
}
//}
/* if (true)
{
} */
//PrizeBox(Prize);
if (UserChoice == UserGuess)
{
cout << "You chose to stay with your original box." << endl;
BoxCheck(UserGuess);
}
else if (UserChoice != UserGuess) //|| UserChoice != BoxReveal)
{
cout << "You decided to switch." << endl;
BoxCheck(UserGuess);
}
/*else if (UserChoice != UserGuess || BoxReveal)
{
cout << "You decided to switch." << endl;
BoxCheck(UserGuess);
} */
else
{
cout << "Your answer was out of the parameters." << endl;
BoxCheck(UserGuess);
}
//BoxCheck(UserGuess);
system("pause");
return 0;
}
void PrizeBox(int& PrizeB)
{
srand(time(NULL));
PrizeB = rand() % 3 + 1;
//Prize = rand() % 3 + 1;
//cin >> PrizeB;
/* BoxReveal = !(PrizeBox || boxChoice);
cout << "Here is one of the box's opened! " << boxReveal << " ." << endl;
BSwitch = (boxChoice || boxReveal); */
}
void BoxCheck(int UserChoice)
{
if (UserChoice == PrizeB)
{
cout << "WOW YOU WON!!!!" << endl;
}
else if (UserChoice != PrizeB)
{
cout << "Sorry you got a red snapper" << endl;
}
else
{
cout << "Sorry you got a red snapper" << endl;
}
}
A simple way to switch between application in Windows 8 is "Swipe Right" using the touch screen (put down one finger in the left side of the screen and move it a few pixels to the right). I'm trying to simulate this behavior using Touch Injection API but the switch app behavior is not invoked although the feedback shows the swipe is actually happening.
Here's the code I ran:
int main()
{
//Screen Resolution 1366x768
BOOL ret = TRUE;
//init
InitializeTouchInjection(10, TOUCH_FEEDBACK_INDIRECT);
POINTER_TOUCH_INFO contact = {0};
memset(&contact, 0, sizeof(POINTER_TOUCH_INFO));
contact.pointerInfo.pointerType = PT_TOUCH;
contact.pointerInfo.pointerId = 0;
//set start point (a point at the left side of the screen)
contact.pointerInfo.ptPixelLocation.y = 768/2;
contact.pointerInfo.ptPixelLocation.x = 0;
contact.touchFlags = TOUCH_FLAG_NONE;
contact.touchMask = TOUCH_MASK_CONTACTAREA | TOUCH_MASK_ORIENTATION | TOUCH_MASK_PRESSURE;
contact.orientation = 90;
contact.pressure = 32000;
contact.rcContact.top = contact.pointerInfo.ptPixelLocation.y -2;
contact.rcContact.bottom = contact.pointerInfo.ptPixelLocation.y + 2;
contact.rcContact.left = contact.pointerInfo.ptPixelLocation.x -2;
contact.rcContact.right = contact.pointerInfo.ptPixelLocation.x + 2;
//set flags for "start touch"
contact.pointerInfo.pointerFlags = POINTER_FLAG_DOWN | POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT;
ret = InjectTouchInput(1, &contact);
cout << "X: " << contact.pointerInfo.ptPixelLocation.x << " Y: " << contact.pointerInfo.ptPixelLocation.y << endl;
if(ret != TRUE)
{
cout << "Error 1: " << GetLastError() <<endl;
return 1;
}
for(int i=0; i<100; i++)
{
//set flags for "update"
contact.pointerInfo.pointerFlags = POINTER_FLAG_UPDATE | POINTER_FLAG_INRANGE | POINTER_FLAG_INCONTACT;
//move the location one pixel right
contact.rcContact.bottom = contact.pointerInfo.ptPixelLocation.x++;
ret = InjectTouchInput(1, &contact);
cout << "X: " << contact.pointerInfo.ptPixelLocation.x << " Y: " << contact.pointerInfo.ptPixelLocation.y << endl;
if(ret != TRUE)
{
cout << "Error 2: " << GetLastError() <<endl;
return 1;
}
}
//set flags for "end touch"
contact.pointerInfo.pointerFlags = POINTER_FLAG_UP;
ret = InjectTouchInput(1, &contact);
cout << "X: " << contact.pointerInfo.ptPixelLocation.x << " Y: " << contact.pointerInfo.ptPixelLocation.y << endl;
if(ret != TRUE)
{
cout << "Error 3: " << GetLastError() <<endl;
return 1;
}
return 0;
}
Is there any idea why this code doesn't behave as expected?
By the way, same happens when trying to "Swipe Left" to open Charms-Bar
The code is written relating on this article
I'm trying to port my IRC bot from Python to C++ and I'm running into some issues with Winsock2. I'm fairly new to sockets in C/C++ and most of this code was pieced together from various tutorials. I keep getting error 10049 and am at a loss. Any help would be appreciated greatly. Thanks!
port_ is set to 6667 and host_ is "irc.rizon.net"
WSADATA wsaData;
int starterr = WSAStartup(MAKEWORD(2,2), &wsaData);
if (starterr != 0) {
std::cout << "Error: " << WSAGetLastError() << " occurred!" << std::endl;
WSACleanup();
return 1;
}
std::cout << "WSAStartup Successful!" << std::endl;
socketfd_ = socket(AF_INET,SOCK_STREAM,IPPROTO_TCP);
if (socketfd_ == INVALID_SOCKET) {
std::cout << "Error: " << WSAGetLastError() << " occurred!" << std::endl;
WSACleanup();
return 1;
}
std::cout << "Socket Creation Successful!" << std::endl;
sockaddr_in anews;
anews.sin_port = htons(port_);
anews.sin_addr.s_addr = inet_addr(host_.c_str());
anews.sin_family = AF_INET;
if (connect(socketfd_,(sockaddr*)&anews, sizeof(anews)) == SOCKET_ERROR) {
std::cout << "Error: " << WSAGetLastError() << " occurred!" << std::endl;
WSACleanup();
return 1;
}
std::cout << "Socket has connected successfuly!" << std::endl;
return 0;
inet_addr() takes a dotted IP address of the form "x.x.x.x" you are passing it the host name.
You can use gethostbyname():
hostent* host;
char* ip;
...
// Get the local host information
host= gethostbyname(host_.c_str());
ip= inet_ntoa(*(struct in_addr *)*host->h_addr_list);
sockaddr_in anews;
anews.sin_port = htons(port_);
anews.sin_addr.s_addr = inet_addr(ip);
anews.sin_family = AF_INET;
...
Or an easier route would be to use getaddrinfo():
struct addrinfo *ai;
if(getaddrinfo(host_.c_str(), "6667", NULL, &ai) != 0)
return 1;
socketfd_ = socket(ai->ai_family, SOCK_STREAM, 0);
if (socketfd_ == INVALID_SOCKET) {
freeaddrinfo(ai);
return 1
}
if (connect(socketfd_, ai->ai_addr, (int)ai->ai_addrlen) == SOCKET_ERROR) {
closesocket(socketfd_);
freeaddrinfo(ai);
return 1;
}
...