how to load images in sdl to a surface in c using IMG_Load? - c

When I run
SDL_Surface* surface = IMG_Load("*image location*");
an error pops up saying
initialization of 'SDL_Surface *' {aka 'struct SDL_Surface *'} from 'int' makes pointer from integer without a cast [-Wint-conversion]
I don't know why this happening because shouldn't IMG_load be returning a pointer
https://www.libsdl.org/projects/SDL_image/docs/SDL_image_11.html
here is the rest of the code
#include <stdio.h>
#include <windows.h>
#include <SDL2\SDL.h>
int WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
int result = SDL_Init(SDL_INIT_EVERYTHING);
SDL_Window *window = SDL_CreateWindow("bruh",SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,500,500,0);
SDL_Renderer *rend = SDL_CreateRenderer(window,-1,SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
SDL_Surface* surface = IMG_Load("*image location*");
SDL_Texture* texture = SDL_CreateTextureFromSurface(rend,surface);
SDL_FreeSurface(surface);
SDL_RenderClear(rend);
SDL_RenderCopy(rend,texture,NULL,NULL);
SDL_RenderPresent(rend);
SDL_Delay(5000);
SDL_DestroyTexture(texture);
SDL_DestroyRenderer(rend);
SDL_DestroyWindow(window);
SDL_Quit();
return 0;
}
how do I fix this error?

IMG_Load belongs to separate libary SDL_image. This function is declared in SDL_image.h, which you didn't include. Add that include and link with appropriate library. In C undeclared functions are allowed via implicit declarations, and implicitly declared function returns int, that's where your error comes from.
Sane compiler will issue a warning about implicit declaration. Warnings are there for a reason, read them and don't suppress warnings unless you're absolutely certain.

Related

How to correctly build code for gcc with netapi32?

I just try build this example for GCC.
But I got several problems.
My system is:
Win10
msys64 with ucrt64 (x86_64-w64-mingw32)
c:\windows\servicing\LCU\Package_for_RollupFix~31bf3856ad364e35~amd64~~19041.2486.1.5\amd64_microsoft-windows-netapi32_31bf3856ad364e35_10.0.19041.2130_none_0202483cb6bb2fc6\f\netapi32.dll
Atom as IDE (I have not VS)
cmd as console
I try: gcc main.c -o main.exe -lnetapi32
But gcc returned:
undefined reference to `WinMain' collect2.exe: error: ld returned 1 exit status
OK - wrong entry point.
Changed wMain in WinMain.
Result:
main.c:11:5: error: conflicting types for 'WinMain'; have 'int(int, wchar_t **)' {aka 'int(int, short unsigned int **)'}
11 | int WinMain(int argc, wchar_t *argv[])
| ^~~~~~~
In file included from c:/msys64/ucrt64/include/windows.h:70,
from main.c:8:
c:/msys64/ucrt64/include/winbase.h:1128:14: note: previous declaration of 'WinMain' with type 'int(struct HINSTANCE__ *, struct HINSTANCE__ *, CHAR *, int)' {aka 'int(struct HINSTANCE__ *, struct HINSTANCE__ *, char *, int)'}
1128 | int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd);
What is wrong?
wmain is an extension by Microsoft, and insofar I know not supported by gcc. You need to change the entry function to a standard int main(int argc, char **args). You can also use WinMain but then you'll have to use int WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)

How to display variables on windows api message box C programming

I'm trying to print out a variable to a message box from Language C
This is my current code
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
#include <Windows.h>
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPreveInstance, LPSTR lpCmdLine, int nCmdShow)
{
srand((unsigned int)time(NULL));
int dice = (rand() % 20) + 1;
char temp[128];
sprintf(temp, "The die shows: %d", dice);
MessageBox(NULL, temp, L"Dice", MB_YESNO);
return 0;
}
my attempt was assigning the string which included a variable and then I putting that assigned string into the MessageBox but whenever I compiled this It will give me a warning saying
error C2220: warning treated as error - no 'object' file generated
warning C4133: 'function': incompatible types - from 'char [128]' to 'LPCWSTR'
warning C4100: 'nCmdShow': unreferenced formal parameter
warning C4100: 'lpCmdLine': unreferenced formal parameter
warning C4100: 'hPreveInstance': unreferenced formal parameter
warning C4100: 'hInstance': unreferenced formal parameter
would there be any solution to this?
I am currently using Visual Studio 2017
MessageBox is actually a macro - there are two versions: MessageBoxA which takes chars and MessageBoxW which takes wide chars. Depending on the default character set, it will take either the A or W version. By default, it takes the W version.
If you go into the project properties, under general, near the bottom of the dialog, there is an entry for character set. By default, it is set to unicode (the W version). Just change this to MBCS (Multi byte character set) and your program should build after you've removed the L from the MessageBox title
Alternatively leave it as Unicode and change the code to the following. Note that you don't need winmain if it is not using the GUI. You can use MessageBox in a console application
int main()
{
srand((unsigned int)time(NULL));
int dice = (rand() % 20) + 1;
wchar temp[128];
wsprintf(temp, L"The die shows: %d", dice);
MessageBox(NULL, temp, L"Dice", MB_YESNO);
return 0;
}
There is a third solution using TCHAR but I'll have to look it up before I post it.
Edit the third solution
If you look in stdafx.h, it has probably already included tchar.h. These are character agnostic definitions. You can use a MessageBox with a C++ Win32 console application.
#include "stdafx.h"
#include <stdlib.h>
#include <time.h>
#include <Windows.h>
int _tmain(int argc, _TCHAR* argv[])
{
srand((unsigned int)time(NULL));
int dice = (rand() % 20) + 1;
TCHAR temp[128];
_stprintf(temp, _T("The die shows: %d"), dice);
MessageBox(NULL, temp, _T("Dice"), MB_YESNO);
return 0;
}

passing argument 1 of ‘gtk_label_set_selectable’ from incompatible pointer type

I trying to learn GTK programming. I wrote a simple program in c. I am trying to pass a pointer to a label to the gtk_label_set_selectable() function, but I get this warning:
#include <gtk/gtk.h>
#include <stdlib.h>
long i = 1;
void showadd(short u,GtkWidget *l)
{
char * s;
s = (char*)malloc(20);
sprintf(s,"%ld",++i);
gtk_label_set_text(GTK_LABEL(l),s);
}
int main (int argc, char *argv[])
{
gtk_init(&argc,&argv);
GtkWidget * win,* lable, * addButton,* SubstractButton,*box;
win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
addButton = gtk_button_new_with_label("+");
lable = gtk_label_new("1");
box = gtk_vbox_new(0,0);
g_signal_connect(win,"delete-event",G_CALLBACK(exit),NULL);
g_signal_connect(addButton,"clicked",G_CALLBACK(showadd),(gpointer)lable);
gtk_label_set_selectable(lable,1); //getting warring here
gtk_container_add(GTK_CONTAINER(win),box);
gtk_box_pack_start(GTK_BOX(box),lable,1,0,0);
gtk_box_pack_start(GTK_BOX(box),addButton,1,0,0);
gtk_widget_show_all(win);
gtk_main();
}
Whene I compile this in eclipse CDT I get this warning in the console :
../src/Calculator.c:35:28: warning: passing argument 1 of ‘gtk_label_set_selectable’ from incompatible pointer type [-Wincompatible-pointer-types]
gtk_label_set_selectable(lable,1);
^~~~~
Should I ignore this warning? If not then what can I do to make it go away? And if yes then how can I make disappear in eclipse.
In GTK you need to take care about the types of your widgets.
Normally only GtkWidget* is used instead of more specialized types because the gtk_*_new() functions also return a pointer to GtkWidget instead of GtkLabel etc.
To use a GtkWidget* in a function expecting a GtkLabel* you need to cast using GTK_LABEL(label).
This is the mechanism you're already using when you call GTK_CONTAINER() and GTK_BOX().

argument of type const char* is incompatible with parameter of type "LPCWSTR"

I am trying to make a simple Message Box in C in Visual Studio 2012, but I am getting
the following error messages
argument of type const char* is incompatible with parameter of type "LPCWSTR"
err LNK2019:unresolved external symbol_main referenced in function_tmainCRTStartup
Here is the source code
#include<Windows.h>
int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow)
{
MessageBox(0,"Hello","Title",0);
return(0);
}
Please Help
Thanks and Regards
To compile your code in Visual C++ you need to use Multi-Byte char WinAPI functions instead of Wide char ones.
Set Project -> Properties -> General -> Character Set option to Use Multi-Byte Character Set
I found it here https://stackoverflow.com/a/33001454/5646315
To make your code compile in both modes, enclose the strings in _T() and use the TCHAR equivalents
#include <tchar.h>
#include <windows.h>
int WINAPI _tWinMain(HINSTANCE hinstance, HINSTANCE hPrevinstance, LPTSTR lpszCmdLine, int nCmdShow)
{
MessageBox(0,_T("Hello"),_T("Title"),0);
return 0;
}
I recently ran in to this issue and did some research and thought I would document some of what I found here.
To start, when calling MessageBox(...), you are really just calling a macro (for backwards compatibility reasons) that is calling either MessageBoxA(...) for ANSI encoding or MessageBoxW(...) for Unicode encoding.
So if you are going to pass in an ANSI string with the default compiler setup in Visual Studio, you can call MessageBoxA(...) instead:
#include<Windows.h>
int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow)
{
MessageBoxA(0,"Hello","Title",0);
return(0);
}
Full documentation for MessageBox(...) is located here: https://msdn.microsoft.com/en-us/library/windows/desktop/ms645505(v=vs.85).aspx
And to expand on what #cup said in their answer, you could use the _T() macro and continue to use MessageBox():
#include<tchar.h>
#include<Windows.h>
int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow)
{
MessageBox(0,_T("Hello"),_T("Title"),0);
return(0);
}
The _T() macro is making the string "character set neutral". You could use this to setup all strings as Unicode by defining the symbol _UNICODE before you build (documentation).
Hope this information will help you and anyone else encountering this issue.
Yes whatever it was it was a wrong tutorial, you need to make it a long byte integer.
Try this:
#include<Windows.h>
int _stdcall WinMain(HINSTANCE hinstance,HINSTANCE hPrevinstance,LPSTR lpszCmdline,int nCmdShow)
{
MessageBox(0,L"Hello",L"Title",0);
return(0);
}

Undefined Reference? But I've implemented the function

The Code
window.h
typedef struct
{
WNDCLASS* wc;
HWND hwnd;
WNDPROC proc;
} PRO_Window;
PRO_Window* PRO_WindowCreate(int width, int height, const char* title);
window.c
I don't think this is important though...
PRO_Window* PRO_WindowCreate(int width, int height, const char* title) { /* code */ }
main.c
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR CmdLine, int nCmdShow)
{
PRO_Window* win = PRO_WindowCreate( 300, 300, "STATIC");
return 0;
}
The Error
GCC
main.o:main.c|| undefined reference to `PRO_WindowCreate(int, int, char const*)'|
* Reduced it to SSCCE
* I am using the windows api
The error with Boarland-C compiler seems to be because of the declaration of wc after the wnd->proc = NULL;. Try to move declaration above initialization statement...
The .h file doesn't have const, while the .c file does.
Your declaration (in window.h):
PRO_Window* PRO_WindowCreate(int width, int height, char* title);
is different then definition (window.c):
PRO_Window* PRO_WindowCreate(int width, int height, const char* title)
Note the last argument.
Looks like a GCC linking error, but you're not showing the command line used to compile, so it's hard to tell.
Also, you seem to be missing to #include the header in both the C files.
You should #include "window.h" file in both in the implementation and the user of the PRO_WindowCreate function.

Resources