[Reserved] ApplePie's SDL Experience

xiaoxiao2021-03-06  40

Original: http://fly19.51.net/applepie/ I found that there is too little content on the Internet to SDL, even I can't find the information about SDL_TTF on Google, so I think I can know what I know. About SDL knowledge and everyone share the knowledge I know is what I have learned or learned from English information, so if I have a wrong place, please point out

As for the home page of SDL to go to Google, I am using anjuta1.1.97 to write SDL, I don't like KDevelop because I select "Compiler and Connector Settings" in the ANJUTA setting menu. In the reference path Add / USR / include / SDL Add / usr / lib to the Library path Add SDL, SDL_IMAGE, SDL_TTF (optional, this TTF is very depressed), pthread (this I don't know if I don't know if this is necessary. Ok, now you can start writing #include "sdl.h" / * all sdl app's need this * / # incdude int main () {printf ("Initializing SDL./N"); / * Initialize defaults, Video and audio * / if ((SDL_INIT (SDL_INIT_VIDEO) == - 1)) {Printf ("Could Not Initialize SDL:% s. / N", SDL_GETERROR ()); EXIT (1);} Printf "SDL Initialized./N"); Printf ("Quiting SDL./N"); / * Shutdown All subsystems * / sdl_quit (); Printf ("Quiting .... / n"); exit (0);} If the above code is compiled, the SDL is no problem.

Let's first talk about the keyboard: void Rungame (void) {SDL_EVENT Event; uint8 * keys; int goloop = 1; while (goloop) {while (SDL_Pollevent (& Event)) {if (event.type == sdl_quit) Return Keys = SDL_GETKEYSTATE (NULL); <--------------------- IF (Keys [SDLK_ESCAPE] == SDL_PRESSED) {<------ ------------- Check the value of a button GOLOOP = 0;}}}} return;} SDL_GetKeyState This function seems to say that they don't encourage them to use this function. Using SDL_POLLEVENT (& Event) directly from Event Direct the value of the keyboard is what he can't return two keys at the same time, if you cut people on the game, there will be problems. One way to set a variable (there is an example on the SDL home page) But I feel that I think SDL_GetKeyState is convenient for the convenience of SDL_GetKeyState. Although there is also a disadvantage that this function must be called, which is the CPU utilization to 100% but who is there What do you care about playing games?

Let's talk about pictures: Compared with a sentence #include "SDL_Image" SDL_Surface is the most one thing I use. Although I haven't much better to it, I found a ready-made function: SDL_SURFACE * LoadImage (char * filename, int I Transparent, uint32 key) {SDL_SURFACE * image, * surface; image = img_load (filename); if (image == null) {fprintf (stderr, "couldn't load iMage% s:% s / n", filename, IMG_GETERROR ()); return NULL;} if (transparent) {SDL_SetColorKey (image, (SDL_SRCCOLORKEY | SDL_RLEACCEL), key);} surface = SDL_DisplayFormat (image); SDL_FreeSurface (image); return (surface);} this function is from sdl The uint32 key in the function on one of the homepage is the original function that I am adding to the original function. It is not used to decide which color is transparent, such as the 0xffff00, SDL_SETCOLORKEY, does not fully understand its role, so here I don't dare to say that there is still a problem although image = img_load (filename); IMAGE is not empty, but Surface = SDL_DISPLAYFORMAT (Image); Surface may also be empty, I don't know, I don't know why the original author is not I don't know if IMG_load (filename); I can use JPG or PNG. I use BMP. In short, I have turned a BMP file into SDL_SURFACE. I want to steal and reduce SDL_FILLRECT's role to see the name Know that it is often used to empty a sdl_surface such as: SDL_FILLRECT (Surface, Null, 0); SDL_BLITSURFACE is used to put an SDL_SURFAC E copy to another lazy to tell the SDL's website SDL_UPDATERECT (Screen, 0, 0, 0, 0); 4 values ​​used to update the screen is the coordinate if it is 0 is updating the entire screen SDL home page There are also some kinds of functions that I don't know how to be cloud.

Here is the problem that the most annoyed SDL_TTF is his role is written on the Surface requires the support of FreeType 2.0 or more http://www.libsdl.org/projects/sdl_ttf/Descen: IF (ttf_init () == - 1) {Printf ("TTF_INIT:% S / N", TTF_Geterror ()); EXIT (2);} Load TTF file: TTF_FONT * font; font = TTF_OpenFont ("font.ttf", 16); <- - This is to see some introduction and then I will feel that he is not written by it. It may be that I misunderstood if (! Font) {printf ("TTF_OpenFont:% S / N", TTF_GETERROR ());} I thought that TTF_OpenFont The path of "font.ttf" is fixed or is determined by SDL_TTF, but later discovers that there must be a relative path or an absolute path. Otherwise, TTF_OpenFont has a problem as long as you don't have direct files in your TTF If there is a paragraph error, it is said that it is a BUG that is misleaded by this introduction. Wasted a lot of time. I am very annoyed. The following is more annoyed: SDL_SURFACE * image; image = ttf_rendertext_solid (font, "abc", COLOR); TTF_RENDERTEXT_SOLID is used to turn the text into a SDL_SURFACE. The first parameter is the FONT third parameter above color is an SDL_Color if it is not unexpected image, IMAGE should have ABC three words but unfortunate is unexpected I have repeatedly checked the accuracy of Font and Color. I don't have any problems. But Image is always null. If you are using SDL on Windows, there should be no such problem but I use redHat9.0 freetype's version is 2.1.4 SDL is 1.25-3 SDL_TTF is 2.0.6-1 later to see the website, not just I have such a problem but did not find a solution I have found (or I found but I didn't understand This problem, this is completed, but can't be depressed! There is no way to use the text in the game to use the text to draw BMP for the problem about SDL_TTF. I have solved the TTF_RENDERTEXT_SOLID if it is not used, but can be used with another function instead of TTF_Rendertext_blended usage, but I only use this function to draw English text I think Chinese should have no problem, there are questions about avoiding TTF_openfont. I have seen other people's source code. Methods of explosion, use first check if the file exists std :: ifstream ifile (fontname); ifile (ifile); IF (ifile. Fail ()) {std :: cout << "File Not Found:" FontName << std :: end1; return err;} font = ttf_openfont (fontname, size); if (font == null) {std: : cout << "OpenFont Error:" << TTF_GETERROR () << std :: endl; return err;} But this can only check if there is an empty file if it is an empty file, but I haven't met. Instead of this problem

转载请注明原文地址:https://www.9cbs.com/read-59902.html

New Post(0)