Troubles porting C++ SDL to Derelict3 in D (D) - sdl

I've gotten Derelict3 working under DMD 2.xx but now I'm having trouble porting my SDL code from C++ to D, the following code gives me this error:
C:\Documents and Settings\Kevin Kowalczyk\My Documents\Code\D\Snippets\Dereli
DL>dmd main.d
main.d(14): Error: undefined identifier SDL_SetVideoMode
main.d(14): Error: undefined identifier SDL_HWSURFACE
main.d(14): Error: undefined identifier SDL_DOUBLEBUF
main.d(15): Error: undefined identifier SDL_WM_SetCaption
main.d(21): Error: cannot implicitly convert expression (SDL_Quit) of type ex
n (C) void function() nothrow to uint
main.d(20): Error: non-final switch statement without a default is deprecated
This is the code:
import std.stdio;
import derelict.sdl2.sdl;
pragma(lib, "DerelictSDL2.lib");
pragma(lib, "DerelictUtil.lib");
SDL_Surface Surf_Display;
bool running = true;
void main() {
DerelictSDL2.load();
SDL_Init(SDL_INIT_EVERYTHING);
SDL_SetVideoMode(640, 480, 32, SDL_HWSURFACE | SDL_DOUBLEBUF);
SDL_WM_SetCaption("Derelict3SDL test", null);
SDL_Event event;
while(running) {
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_Quit:
running = false;
}
}
}
}

Derelict3 uses SDL2, which doesn't have those functions/flags.
Here's the new API: http://wiki.libsdl.org/moin.cgi/CategoryAPI
I think your new init code should be:
SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window* window = SDL_CreateWindow("Derelict3SDL test",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 640, 480,
SDL_WINDOW_FULLSCREEN|SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
This is completely and utterly untested. I'm just copying code from the SDL wiki.

Related

SDL2 opens no window without giving any errors

I'm trying to test a simple SDL2 application. The app runs without any errors. However, no window shows up.
I'm using Ubuntu18.04 on a VMWare machine. I also tried to compile SDL from the source but got the same result. I am unsure if the cause of the problem is related to my OS or because of executing from a virtual machine.
Here is a simple application from docs (I just added SDL_GetNumDisplayModes to make sure the display mode is OK- returns 1)
// Using SDL and standard IO
#include <SDL2/SDL.h>
#include <stdio.h>
// Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
int main(int argc, char *args[])
{
// The window we'll be rendering to
SDL_Window *window = NULL;
// The surface contained by the window
SDL_Surface *screenSurface = NULL;
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
printf("SDL could not initialize! SDL_Error: %s\n", SDL_GetError());
}
else
{
int res = SDL_GetNumDisplayModes(0);
printf("SDL_GetNumDisplayModes: %d\r\n", res);
// Create window
window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL)
{
printf("Window could not be created! SDL_Error: %s\n", SDL_GetError());
}
else
{
// Get window surface
screenSurface = SDL_GetWindowSurface(window);
// Fill the surface white
SDL_FillRect(screenSurface, NULL, SDL_MapRGB(screenSurface->format, 0xFF, 0xFF, 0xFF));
// Update the surface
SDL_UpdateWindowSurface(window);
// Hack to get window to stay up
SDL_Event e;
bool quit = false;
while (quit == false)
{
while (SDL_PollEvent(&e))
{
if (e.type == SDL_QUIT)
{
quit = true;
}
}
}
}
}
// Destroy window
SDL_DestroyWindow(window);
// Quit SDL subsystems
SDL_Quit();
return 0;
}

BadRequest in SDL2 with WSL2

With this minimal example I have a BadRequest error:
#include <SDL.h>
int main( int argc, char* args[] )
{
if(SDL_Init(SDL_INIT_VIDEO) < 0) abort();
SDL_Window *window = SDL_CreateWindow(
"An SDL2 window",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
640,
480,
SDL_WINDOW_OPENGL
);
if(window == NULL) abort();
SDL_Renderer* renderer = SDL_CreateRenderer(
window,
-1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
);
if(renderer == NULL) abort();
SDL_Delay(3000);
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
}
The error is the following
$ g++ $(sdl2-config --cflags) foo.cpp $(sdl2-config --libs)
$ ./a.out
X Error of failed request: BadRequest (invalid request code or no such operation)
Major opcode of failed request: 149 (GLX)
Minor opcode of failed request: 16 (X_GLXVendorPrivate)
Serial number of failed request: 197
Current serial number in output stream: 198
It seems the issue appears at the SDL_CreateRenderer function. Creating the window works which confirms my X server works too.

Access violation at SDL_CreateRenderer

I use vc++ 2010 express and currently working on a project but when I try to use SDL_CreateRenderer function I get this error:
First-chance exception at 0x6c8037be in Oyun Projem.exe: 0xC0000005: Access violation reading location 0x00000010.
Unhandled exception at 0x6c8037be in Oyun Projem.exe: 0xC0000005: Access violation reading location 0x00000010.
The program '[320] Oyun Projem.exe: Native' has exited with code -1073741819 (0xc0000005).
at this line:
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
here is the code
#include "stdafx.h"
int main(int argc, char* argv[])
{
SDL_Init(SDL_INIT_VIDEO);
SDL_Window *window;
SDL_Renderer *renderer;
window = NULL;
window = SDL_CreateWindow("My first RPG!", 100, 100, 100, 100, SDL_WINDOW_SHOWN);
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
return 0;
}
Here is what I see when debugging:
SDL_CreateWindow returns NULL if there is a failure. Your code needs to check for this.
// Your code:
window = SDL_CreateWindow("My first RPG!", 100, 100, 100, 100, SDL_WINDOW_SHOWN);
// Add the following:
// Check that the window was successfully made
if (window == NULL) {
// In the event that the window could not be made...
printf("Could not create window: %s\n", SDL_GetError());
return 1;
}

'SDL_SetVideoMode': identifier not found

I have a problem with SDL lib. I'm using VS2012 Ultimate and i was actually using this tutorial: http://lazyfoo.net/tutorials/SDL/01_hello_SDL/index2.php to set everything and i did it step by step few times, but I still have problems this is my code, very simple:
#include <iostream>
#include <SDL.h>
SDL_Surface * ekran = NULL;
int main (int argc, char *args [] )
{
SDL_Init( SDL_INIT_EVERYTHING );
ekran = SDL_SetVideoMode( 640, 480, 32, SDL_SWSURFACE );
SDL_Flip( ekran );
SDL_Delay( 2000 );
SDL_Quit();
return 0;
}
and im having this errors:
error C3861: 'SDL_SetVideoMode': identifier not found
error C3861: 'SDL_Flip': identifier not found
Here below is an example how to replace SDL_SetVideoMode() in SDL2. The old way to init SDL is commented and left along with the new way for comparison purposes. Basically, SDL2 creates a window with a title, then a surface attached to it, while SDL1 creates a surface alone and then calls the window manager to give it a name.
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
fprintf(stderr, "SDL video init failed: %s\n", SDL_GetError());
return 1;
}
// SDL_Surface *screenSurface = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_SWSURFACE);
SDL_Window* window = NULL;
SDL_Surface* screenSurface = NULL;
window = SDL_CreateWindow("Sphere Rendering",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == NULL) {
fprintf(stderr, "Window could not be created: %s\n", SDL_GetError());
return 1;
}
screenSurface = SDL_GetWindowSurface(window);
if (!screenSurface) {
fprintf(stderr, "Screen surface could not be created: %s\n", SDL_GetError());
SDL_Quit();
return 1;
}
// SDL_WM_SetCaption("Sphere Rendering", NULL);
Take a look at that tutorial page again. Your code does not match it (e.g. SDL_SetVideoMode() no longer exists). Your code uses SDL 1.2 and the (updated) tutorial uses SDL 2.0. Are you using an old cached version of that page?

glew not initializing with SDL2

I've been trying to get GLew 1.10 to play nicely with SDL 2.0.3, but GLew won't initialize.
The problem I'm having is that GLew 1.10 requires a function GLEWContext* glewGetContext().
I've tried to use a the same solution used for GLew 1.10 with GLFW3, where you use a struct to handle the window and GLew context, but that method doesn't work with SDL2.
The 2 errors I'm receiving is this which points to glewInit():
C3861: 'glewGetContext': identifier not found
Intellisense: identifier "glewGetContext is undefined
code:
// Create window
_screen = SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
800, 600, SDL_WINDOW_OPENGL | SDL_WINDOW_SHOWN);
/* Create Context */
_mainContext = SDL_GL_CreateContext(_screen);
/* swap syncronized */
SDL_GL_SetSwapInterval(0);
// Initialize GLew 1.10
glewExperimental = GL_TRUE;
GLenum glewError = glewInit(); <------------- error
if (glewError != GLEW_OK)
printf("Error with GLew. SDL Error: %s\n", SDL_GetError());