鱼C论坛

 找回密码
 立即注册
查看: 1729|回复: 5

[已解决]在虚拟机linux里面怎么用c编写带图形界面的程序

[复制链接]
发表于 2022-8-30 14:35:28 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
跟着小甲鱼的带你学c带你飞学了一遍,47还是48以后就没题目了,然后我又自己看了一遍c primer plus,现在想自己动手写点东西,发现自己好像只会在那个黑色方框里敲代码,在百度上查了好像是要安装一个叫什么图形界面的东西才能编写像俄罗斯方块,贪吃蛇这种带图形的程序,想问问论坛里的大佬们,在虚拟机的linux系统里面怎么安装这个东西(虚拟机安装linux就是跟着论坛里面那个系列教程弄的,我现在还有点懵,只会用,不会装)
最佳答案
2022-8-30 14:47:22
试试sdl

  1. $ ls
  2. main.c  Makefile  pic.bmp
  3. $ cat main.c
  4. #include <SDL2/SDL.h>

  5. int main(void) {
  6.     SDL_Init(SDL_INIT_EVERYTHING);
  7.     SDL_Window *win = SDL_CreateWindow("hello world", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1440, 900, SDL_WINDOW_SHOWN);
  8.     SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  9.     SDL_Surface *bmp = SDL_LoadBMP("pic.bmp");
  10.     SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
  11.     SDL_FreeSurface(bmp);
  12.     SDL_RenderCopy(ren, tex, NULL, NULL);
  13.     SDL_RenderPresent(ren);

  14.     while(1) {
  15.         SDL_Event event;
  16.         SDL_WaitEvent(&event);
  17.         SDL_Log("Event type is %u\n", event.type);
  18.         switch(event.type) {
  19.             case SDL_QUIT:
  20.                 goto QUIT_LOOP;
  21.         }
  22.     }
  23. QUIT_LOOP:
  24.     SDL_DestroyTexture(tex);
  25.     SDL_DestroyRenderer(ren);
  26.     SDL_DestroyWindow(win);
  27.     SDL_Quit();
  28.     return 0;
  29. }
  30. $ cat Makefile
  31. main: main.o
  32.         gcc -g -Wall -o $@ $^ `pkg-config --libs sdl2`

  33. clean:
  34.         rm -f *.o main
  35. $ make
  36. cc    -c -o main.o main.c
  37. gcc -g -Wall -o main main.o `pkg-config --libs sdl2`
  38. $ ls
  39. main  main.c  main.o  Makefile  pic.bmp
  40. $ pacman -Qi sdl2
  41. Name            : sdl2
  42. Version         : 2.0.22-2
  43. Description     : A library for portable low-level access to a video framebuffer, audio output, mouse, and keyboard
  44.                   (Version 2)
  45. Architecture    : x86_64
  46. URL             : https://www.libsdl.org
  47. Licenses        : MIT
  48. Groups          : None
  49. Provides        : None
  50. Depends On      : glibc  libxext  libxrender  libx11  libgl  libxcursor  hidapi  libusb
  51. Optional Deps   : alsa-lib: ALSA audio driver [installed]
  52.                   libpulse: PulseAudio audio driver [installed]
  53.                   jack: JACK audio driver [installed]
  54.                   pipewire: PipeWire audio driver
  55.                   libdecor: Wayland client decorations
  56. Required By     : ffmpeg  ffmpeg4.4  qemu-audio-sdl  qemu-ui-sdl  sdl12-compat  sdl2_image
  57. Optional For    : None
  58. Conflicts With  : None
  59. Replaces        : None
  60. Installed Size  : 3.70 MiB
  61. Packager        : Sven-Hendrik Haase <svenstaro@gmail.com>
  62. Build Date      : Mon 23 May 2022 10:18:28 AM CST
  63. Install Date    : Thu 02 Jun 2022 10:03:26 PM CST
  64. Install Reason  : Installed as a dependency for another package
  65. Install Script  : No
  66. Validated By    : Signature

  67. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2022-8-30 14:47:22 | 显示全部楼层    本楼为最佳答案   
试试sdl

  1. $ ls
  2. main.c  Makefile  pic.bmp
  3. $ cat main.c
  4. #include <SDL2/SDL.h>

  5. int main(void) {
  6.     SDL_Init(SDL_INIT_EVERYTHING);
  7.     SDL_Window *win = SDL_CreateWindow("hello world", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, 1440, 900, SDL_WINDOW_SHOWN);
  8.     SDL_Renderer *ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
  9.     SDL_Surface *bmp = SDL_LoadBMP("pic.bmp");
  10.     SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
  11.     SDL_FreeSurface(bmp);
  12.     SDL_RenderCopy(ren, tex, NULL, NULL);
  13.     SDL_RenderPresent(ren);

  14.     while(1) {
  15.         SDL_Event event;
  16.         SDL_WaitEvent(&event);
  17.         SDL_Log("Event type is %u\n", event.type);
  18.         switch(event.type) {
  19.             case SDL_QUIT:
  20.                 goto QUIT_LOOP;
  21.         }
  22.     }
  23. QUIT_LOOP:
  24.     SDL_DestroyTexture(tex);
  25.     SDL_DestroyRenderer(ren);
  26.     SDL_DestroyWindow(win);
  27.     SDL_Quit();
  28.     return 0;
  29. }
  30. $ cat Makefile
  31. main: main.o
  32.         gcc -g -Wall -o $@ $^ `pkg-config --libs sdl2`

  33. clean:
  34.         rm -f *.o main
  35. $ make
  36. cc    -c -o main.o main.c
  37. gcc -g -Wall -o main main.o `pkg-config --libs sdl2`
  38. $ ls
  39. main  main.c  main.o  Makefile  pic.bmp
  40. $ pacman -Qi sdl2
  41. Name            : sdl2
  42. Version         : 2.0.22-2
  43. Description     : A library for portable low-level access to a video framebuffer, audio output, mouse, and keyboard
  44.                   (Version 2)
  45. Architecture    : x86_64
  46. URL             : https://www.libsdl.org
  47. Licenses        : MIT
  48. Groups          : None
  49. Provides        : None
  50. Depends On      : glibc  libxext  libxrender  libx11  libgl  libxcursor  hidapi  libusb
  51. Optional Deps   : alsa-lib: ALSA audio driver [installed]
  52.                   libpulse: PulseAudio audio driver [installed]
  53.                   jack: JACK audio driver [installed]
  54.                   pipewire: PipeWire audio driver
  55.                   libdecor: Wayland client decorations
  56. Required By     : ffmpeg  ffmpeg4.4  qemu-audio-sdl  qemu-ui-sdl  sdl12-compat  sdl2_image
  57. Optional For    : None
  58. Conflicts With  : None
  59. Replaces        : None
  60. Installed Size  : 3.70 MiB
  61. Packager        : Sven-Hendrik Haase <svenstaro@gmail.com>
  62. Build Date      : Mon 23 May 2022 10:18:28 AM CST
  63. Install Date    : Thu 02 Jun 2022 10:03:26 PM CST
  64. Install Reason  : Installed as a dependency for another package
  65. Install Script  : No
  66. Validated By    : Signature

  67. $
复制代码
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-30 14:54:02 | 显示全部楼层
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-8-30 22:45:28 | 显示全部楼层
人造人 发表于 2022-8-30 14:54
https://cn.bing.com/search?form=MOZLBR&pc=MOZI&q=centos+sdl2

十分感谢!距离我跟着论坛里的教程在虚拟机里安装linux并配置相关的设置到现在大概四个月了,我早忘了那些乱七八糟的命令,今天再用,我第一次感觉到linux的强大
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2022-8-30 22:50:06 | 显示全部楼层
人造人 发表于 2022-8-30 14:54
https://cn.bing.com/search?form=MOZLBR&pc=MOZI&q=centos+sdl2

大佬,我能不能再问个问题,我按照下面那个连接安装了sdl2,但是你第一个发的那个代码是干嘛,我没看懂
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2022-8-30 22:52:42 | 显示全部楼层
gbdx 发表于 2022-8-30 22:50
大佬,我能不能再问个问题,我按照下面那个连接安装了sdl2,但是你第一个发的那个代码是干嘛,我没看懂

一个sdl的测试程序,显示一张图片
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-24 18:20

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表